LeetCode - Move Zeros
程序员文章站
2024-02-17 10:32:34
...
原题网址:
https://leetcode.com/problems/move-zeroes/#/solutions
题解:
令不为0的数从原数列首位依次向后放,后面的数位补零。
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
cur = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[cur] = nums[i]
cur += 1
while(cur<len(nums)):
nums[cur]=0
cur += 1