首页 > 其他 > 详细

leetcode-easy-array-283 move zeros

时间:2019-06-06 15:59:04      阅读:129      评论:0      收藏:0      [点我收藏+]

mycode  77.24%

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        pos = 0
        for i in range(len(nums)):
            if nums[i] != 0 :
                nums[pos] = nums[i]
                pos += 1
        nums[pos:] = [0]*len(nums[pos:])

 

参考:

思路类似于

26-Remove Duplicates from Sorted Array

def moveZeros(nums):
    j = 0   # 记录非零元素应该换到第几个位置
    for i in range(len(nums)):
        if nums[i] != 0:
            nums[j], nums[i] = nums[i], nums[j]
            j += 1
    return nums
print(moveZeros([1,0,1,0,3,12]))

 

leetcode-easy-array-283 move zeros

原文:https://www.cnblogs.com/rosyYY/p/10985351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!