码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 283 Move Zeroes 解题报告

时间:2019-03-25 10:23:43      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:nta   相对   解题报告   while   list   range   思路   def   turn   

题目要求

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.

题目分析及思路

给定一个数组,要求将这个数组中所有的0移到数组末尾并保持非零元素相对位置不变。可以先得到零元素的个数,然后循环将零进行移除和在末尾添加。

python代码 

class Solution:

    def moveZeroes(self, nums: List[int]) -> None:

        """

        Do not return anything, modify nums in-place instead.

        """

        zeros = nums.count(0)

        for _ in range(zeros):

            nums.remove(0)

            nums.append(0)

        

        

 

LeetCode 283 Move Zeroes 解题报告

标签:nta   相对   解题报告   while   list   range   思路   def   turn   

原文地址:https://www.cnblogs.com/yao1996/p/10592129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!