LeetCode問題解-python 283.移動ゼロMove Zeroes(Easy)

5025 ワード

LeetCode問題解-python
  • 283.移動ゼロMove Zeroes(Easy)
  • pythonで比較的簡単
  • github
  • タイトルコード(python 3)
  • 283.移動ゼロMove Zeroes(Easy)
    配列numsが与えられ、ゼロ以外の要素の相対的な順序を維持しながら、すべての0を配列の末尾に移動する関数が記述される.
    例:
    入力:[0,1,0,3,12]出力:[1,3,12,0,0]説明:
    元の配列で操作する必要があります.追加の配列はコピーできません.操作回数をできるだけ減らす.
    pythonで比較的簡単
    間違ったところがあったら指摘してくださいね
    github
    リンク:https://github.com/seattlegirl/leetcode/blob/master/move-zeros.py.
    タイトルコード(python 3)
    #coding=utf-8
    """
    283.    
    
           nums,          0         ,             。
    
      :
    
      : [0,1,0,3,12]
      : [1,3,12,0,0]
      :
    
             ,         。
            。
    """
    
    class Solution:
        def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            n=nums.count(0) #        0     
            for i in range(n):
                nums.remove(0) #     0       
            nums.extend([0]*n) #                        (           )。
            return nums
    
    if __name__ == "__main__":
        print Solution().moveZeroes([0,1,0,3,12])