# Leetcode 283:移动零（最详细解决方案）

输入: [0,1,0,3,12]



1. 必须在原数组上操作，不能拷贝额外的数组。
2. 尽量减少操作次数。

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

nonZeroElements_len = len(nonZeroElements)
for i in range(nonZeroElements_len):
nums[i] = nonZeroElements[i]

nums_len = len(nums)
for i in range(nonZeroElements_len, nums_len):
nums[i] = 0


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

nums_len = len(nums)
for i in range(k, nums_len):
nums[i] = 0


class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
for j in range(nums.count(0)):
nums.remove(0)
nums.append(0)


class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
k = 0
for i, num in enumerate(nums):
if num != 0:
nums[i], nums[k] = nums[k], nums[i]
k += 1


class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
k = 0
for i, num in enumerate(nums):
if num != 0:
if i != k:
nums[i], nums[k] = nums[k], nums[i]
k += 1