Skip to content

Commit 53e6b96

Browse files
Merge pull request #2 from janeyziqinglin/janeyziqinglin-patch-2
Update 0027.移除元素.md
2 parents 690e05d + f49b2e4 commit 53e6b96

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

problems/0027.移除元素.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,24 @@ class Solution {
183183

184184
Python:
185185

186-
```python
186+
```python3
187187
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
208204
```
209205

210206

0 commit comments

Comments
 (0)