feat:修改 27 题 python 代码格式错误问题并增加快慢指针的解法

This commit is contained in:
wangshihua
2022-11-16 09:27:42 +08:00
parent 6d75b123f5
commit 6772a996bb

View File

@ -196,7 +196,7 @@ class Solution {
Python
```python3
```python 3
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
if nums is None or len(nums)==0:
@ -216,6 +216,24 @@ class Solution:
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