We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3fd4b81 commit 1fb8518Copy full SHA for 1fb8518
Remove Element/Remove_Element.py
@@ -0,0 +1,30 @@
1
+# 第一种思路,利用python
2
+# 36ms 99.79%
3
+class Solution:
4
+ def removeElement(self, nums, val):
5
+ """
6
+ :type nums: List[int]
7
+ :type val: int
8
+ :rtype: int
9
10
+ while val in nums:
11
+ nums.remove(val)
12
+ return len(nums)
13
+
14
+# 第二种思路,使用双指针法,控制程序的时间复杂度为线性
15
+# 40ms 77.81%
16
17
18
19
20
21
22
23
+ i, j = 0, len(nums) - 1
24
+ while i <= j :
25
+ if nums[i] == val:
26
+ nums[i], nums[j] = nums[j], nums[i]
27
+ j -= 1
28
+ else:
29
+ i += 1
30
+ return j + 1
0 commit comments