mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
feat:修改 27 题 python 代码格式错误问题并增加快慢指针的解法
This commit is contained in:
@ -196,7 +196,7 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python3
|
```python 3
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeElement(self, nums: List[int], val: int) -> int:
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
if nums is None or len(nums)==0:
|
if nums is None or len(nums)==0:
|
||||||
@ -216,6 +216,24 @@ class Solution:
|
|||||||
return l+1
|
return l+1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``` python 3
|
||||||
|
class Solution:
|
||||||
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
|
# 快慢指针
|
||||||
|
fast = 0 # 快指针
|
||||||
|
slow = 0 # 慢指针
|
||||||
|
size = len(nums)
|
||||||
|
while fast < size: # 不加等于是因为,a = size 时,nums[a] 会越界
|
||||||
|
# slow 用来收集不等于 val 的值,如果 fast 对应值不等于 val,则把它与 slow 替换
|
||||||
|
if nums[fast] != val:
|
||||||
|
nums[slow] = nums[fast]
|
||||||
|
slow += 1
|
||||||
|
fast += 1
|
||||||
|
return slow
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user