Skip to content

Commit a908463

Browse files
Merge pull request youngyangyang04#2451 from heroding77/master
添加0027移除元素 python3 版本
2 parents c342d7e + bf863d2 commit a908463

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/0027.移除元素.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,27 @@ class Solution:
253253

254254
```
255255

256+
``` python 3
257+
# 相向双指针法
258+
# 时间复杂度 O(n)
259+
# 空间复杂度 O(1)
260+
class Solution:
261+
def removeElement(self, nums: List[int], val: int) -> int:
262+
n = len(nums)
263+
left, right = 0, n - 1
264+
while left <= right:
265+
while left <= right and nums[left] != val:
266+
left += 1
267+
while left <= right and nums[right] == val:
268+
right -= 1
269+
if left < right:
270+
nums[left] = nums[right]
271+
left += 1
272+
right -= 1
273+
return left
274+
275+
```
276+
256277
### Go:
257278

258279
```go

0 commit comments

Comments
 (0)