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 5a467c2 commit d1e9630Copy full SHA for d1e9630
problems/0027.移除元素.md
@@ -144,15 +144,28 @@ class Solution {
144
145
Python:
146
147
-```python
+```python3
148
class Solution:
149
- def removeElement(self, nums: List[int], val: int) -> int:
150
- i,n = 0,len(nums)
151
- for j in range(n):
152
- if nums[j] != val:
153
- nums[i] = nums[j]
154
- i += 1
155
- return i
+ """双指针法
+ 时间复杂度:O(n)
+ 空间复杂度:O(1)
+ """
+
+ @classmethod
+ def removeElement(cls, nums: List[int], val: int) -> int:
156
+ fast = slow = 0
157
158
+ while fast < len(nums):
159
160
+ if nums[fast] != val:
161
+ nums[slow] = nums[fast]
162
+ slow += 1
163
164
+ # 当 fast 指针遇到要删除的元素时停止赋值
165
+ # slow 指针停止移动, fast 指针继续前进
166
+ fast += 1
167
168
+ return slow
169
```
170
171
0 commit comments