mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -253,6 +253,27 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
``` python 3
|
||||
# 相向双指针法
|
||||
# 时间复杂度 O(n)
|
||||
# 空间复杂度 O(1)
|
||||
class Solution:
|
||||
def removeElement(self, nums: List[int], val: int) -> int:
|
||||
n = len(nums)
|
||||
left, right = 0, n - 1
|
||||
while left <= right:
|
||||
while left <= right and nums[left] != val:
|
||||
left += 1
|
||||
while left <= right and nums[right] == val:
|
||||
right -= 1
|
||||
if left < right:
|
||||
nums[left] = nums[right]
|
||||
left += 1
|
||||
right -= 1
|
||||
return left
|
||||
|
||||
```
|
||||
|
||||
### Go:
|
||||
|
||||
```go
|
||||
|
Reference in New Issue
Block a user