力扣 283. 移动零

双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def moveZeroes(nums):
"""
Do not return anything, modify nums in-place instead.
"""
s, e, L = 0, 0, len(nums)
while s < L and e < L:
# 找0值
while s < e and nums[s] != 0: s += 1
# 找非0值
while e < L and nums[e] == 0: e += 1
if s < L and e < L:
nums[s], nums[e] = nums[e], nums[s]
s += 1
e += 1
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors:2819 | Views:3142
  • 小站在各种崩坏中坚持了: 1895天16小时34分54秒

请我喝杯咖啡吧~

支付宝
微信