283. Move Zeroes
這個題目有點陷阱,題目要求要把零搬到最後,不過用這個角度去想的話,題目真的不好想,比較好做的做法需要有反面的想法:
把所有的零搬到後面 == 把所有非零的數字搬到前面
TODO
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
slow = 0
fast = 0
while fast < len(nums):
if nums[fast] != 0:
nums[slow] = nums[fast]
slow += 1
fast += 1
while slow < len(nums):
nums[slow] = 0
slow += 1