思路
我们可以用python的list comprehension来取出所以非0的元素,而且这样取出来会保持原有的相对顺序,再统计先后变化的长度,补上相应的0即可
代码
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
a = [i for i in nums if i!=0]
delta = len(nums)-len(a)
zero = [0 for i in range(delta)]
nums[:] = a[:] + zero[:]
ps.这才是pythonic的代码啊哈哈哈哈哈哈哈