File tree Expand file tree Collapse file tree 1 file changed +17
-21
lines changed Expand file tree Collapse file tree 1 file changed +17
-21
lines changed Original file line number Diff line number Diff line change @@ -183,28 +183,24 @@ class Solution {
183
183
184
184
Python:
185
185
186
- ``` python
186
+ ``` python3
187
187
class Solution :
188
- """ 双指针法
189
- 时间复杂度:O(n)
190
- 空间复杂度:O(1)
191
- """
192
-
193
- @ classmethod
194
- def removeElement (cls , nums : List[int ], val : int ) -> int :
195
- fast = slow = 0
196
-
197
- while fast < len (nums):
198
-
199
- if nums[fast] != val:
200
- nums[slow] = nums[fast]
201
- slow += 1
202
-
203
- # 当 fast 指针遇到要删除的元素时停止赋值
204
- # slow 指针停止移动, fast 指针继续前进
205
- fast += 1
206
-
207
- return slow
188
+ def removeElement (self , nums : List[int ], val : int ) -> int :
189
+ if nums is None or len (nums)== 0 :
190
+ return 0
191
+ l= 0
192
+ r= len (nums)- 1
193
+ while l< r:
194
+ while (l< r and nums[l]!= val):
195
+ l+= 1
196
+ while (l< r and nums[r]== val):
197
+ r-= 1
198
+ nums[l], nums[r]= nums[r], nums[l]
199
+ print (nums)
200
+ if nums[l]== val:
201
+ return l
202
+ else :
203
+ return l+ 1
208
204
```
209
205
210
206
You can’t perform that action at this time.
0 commit comments