-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283.移动零.py
43 lines (34 loc) · 1.33 KB
/
283.移动零.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# @lc app=leetcode.cn id=283 lang=python3
#
# [283] 移动零
#
from typing import List
# @lc code=start
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# 设置一个变量,用来指向经过一系列操作后数组中所有为 0 元素的第一个位置上
# 一开始默认在索引为 0 的位置
slow = 0
# 从头到尾遍历数组
# 遍历完毕之后,slow 指向了一个为 0 的元素,或者如果数组中不存在 0 ,就和 fast 一样,超过了数组的范围
for fast in range(len(nums)) :
# 在遍历过程中,如果发现访问的元素是非 0 元素
# 说明 slow 不在正确的位置上,需要向后移动,寻找合适的位置
if nums[fast] != 0:
# 这个时候,原先 slow 的值需要被 fast 的值覆盖
nums[slow] = nums[fast]
# slow 需要向后移动,寻找合适的位置
slow += 1
# 接下来,只需要把 slow 极其后面所有的元素都设置为 0 就行
for i in range(slow,len(nums)) :
# 都设置为 0
nums[i] = 0
# @lc code=end
s = Solution()
ls = [0, 1, 0, 3, 12]
s.moveZeroes(ls)
print(ls)