Leetcode 283.移動零 By Python

思路python

咱們能夠用python的list comprehension來取出因此非0的元素,並且這樣取出來會保持原有的相對順序,再統計前後變化的長度,補上相應的0便可code

代碼io

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的代碼啊哈哈哈哈哈哈哈class

相關文章
相關標籤/搜索