欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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
相关标签: leetcode