283. Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

 

 

 

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

 

posted @ 2017-10-27 14:18  bernieloveslife  阅读(110)  评论(0编辑  收藏  举报