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