mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
update 027.移除元素 python代码
This commit is contained in:
@ -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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user