mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0027.移除元素.md
This commit is contained in:
@ -198,6 +198,7 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
``` python 3
|
``` python 3
|
||||||
|
(版本一)快慢指针法
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeElement(self, nums: List[int], val: int) -> int:
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
# 快慢指针
|
# 快慢指针
|
||||||
@ -213,7 +214,21 @@ class Solution:
|
|||||||
return slow
|
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:
|
Go:
|
||||||
|
Reference in New Issue
Block a user