update 027.移除元素 python代码

This commit is contained in:
Yuhao Ju
2022-11-22 10:13:52 +08:00
committed by GitHub
parent 27f8efa24c
commit cbe3bcf50a

View File

@ -199,21 +199,15 @@ Python
```python3
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
if nums is None or len(nums)==0:
return 0
l=0
r=len(nums)-1
while l<r:
while(l<r and nums[l]!=val):
l+=1
while(l<r and nums[r]==val):
r-=1
nums[l], nums[r]=nums[r], nums[l]
print(nums)
if nums[l]==val:
return l
else:
return l+1
# 快指针遍历元素
fast = 0
# 慢指针记录位置
slow = 0
for fast in range(len(nums)):
if nums[fast] != val:
nums[slow] = nums[fast]
slow += 1
return slow
```