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 @@ -173,28 +173,24 @@ class Solution {
173
173
174
174
Python:
175
175
176
- ``` python
176
+ ``` python3
177
177
class Solution :
178
- """ 双指针法
179
- 时间复杂度:O(n)
180
- 空间复杂度:O(1)
181
- """
182
-
183
- @ classmethod
184
- def removeElement (cls , nums : List[int ], val : int ) -> int :
185
- fast = slow = 0
186
-
187
- while fast < len (nums):
188
-
189
- if nums[fast] != val:
190
- nums[slow] = nums[fast]
191
- slow += 1
192
-
193
- # 当 fast 指针遇到要删除的元素时停止赋值
194
- # slow 指针停止移动, fast 指针继续前进
195
- fast += 1
196
-
197
- return slow
178
+ def removeElement (self , nums : List[int ], val : int ) -> int :
179
+ if nums is None or len (nums)== 0 :
180
+ return 0
181
+ l= 0
182
+ r= len (nums)- 1
183
+ while l< r:
184
+ while (l< r and nums[l]!= val):
185
+ l+= 1
186
+ while (l< r and nums[r]== val):
187
+ r-= 1
188
+ nums[l], nums[r]= nums[r], nums[l]
189
+ print (nums)
190
+ if nums[l]== val:
191
+ return l
192
+ else :
193
+ return l+ 1
198
194
```
199
195
200
196
You can’t perform that action at this time.
0 commit comments