Update 0027.移除元素.md

This commit is contained in:
jianghongcheng
2023-05-03 15:06:11 -05:00
committed by GitHub
parent 9617a8b3fe
commit 424d5c224f

View File

@ -198,6 +198,7 @@ Python
``` python 3
(版本一)快慢指针法
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# 快慢指针
@ -213,7 +214,21 @@ class Solution:
return slow
```
``` python 3
(版本二)暴力递归法
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i, l = 0, len(nums)
while i < l:
if nums[i] == val: # 找到等于目标值的节点
for j in range(i+1, l): # 移除该元素,并将后面元素向前平移
nums[j - 1] = nums[j]
l -= 1
i -= 1
i += 1
return l
```
Go