Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2451 from heroding77/master
Browse files Browse the repository at this point in the history
添加0027移除元素 python3 版本
  • Loading branch information
youngyangyang04 authored Mar 15, 2024
2 parents c342d7e + bf863d2 commit a908463
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a908463

Please sign in to comment.