mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +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:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user