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.

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
j = 0 for i in range(len(nums)):
if nums[i] != 0:
nums[j] = nums[i]
j += 1 nums[j:] = [0] * (i - j + 1)

  

[LeetCode&Python] Problem 283. Move Zeroes的更多相关文章

  1. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  2. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  3. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  4. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  5. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  6. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

  7. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

  8. 283. Move Zeroes(C++)

    283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...

  9. 283. Move Zeroes@python

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

随机推荐

  1. Qt Widgets——子区域和子窗口

    QMdiArea 一般使用于主窗口QMainWindow,用于容纳多个子窗口QMdiSubWindow qt creator 3.0的设计师有MdiArea可直接拖入使用. 界面如下,图中灰色框即是个 ...

  2. vue-router 路由钩子(hook)

    钩子(hook)—劫持机制 路由钩子(守卫钩子): 1.全局钩子2.某个路由独享的钩子3.组件内钩子 三种路由钩子中都涉及到了三个参数,官方(https://router.vuejs.org/zh-c ...

  3. Python isspace() 方法检测字符串是否只由空格组成。

  4. js之close()方法

    .close()方法只适用于通过window.open()打开的弹出窗口.对于浏览器的主窗口,如果没有得到用户允许是不能关闭的.不过,弹出窗口可以调用top.close()在不经用户允许的情况下关闭自 ...

  5. webpack+typescript

    1. npm install --save typescript 2. npm install --save ts-loader webpack.config.js module.exports = ...

  6. 逆袭之旅DAY16.东软实训.Oracle.修改用户

    2018-07-12 15:49:51

  7. 内联函数inline的用法

    一.什么是内联函数 在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗.为了解决这个问题,特别的引入了inline修饰符,表示为内联函数.  栈空间就是指放 ...

  8. (路-莫)-Python基础一

    一,Python介绍 1,python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打 ...

  9. 改变Cube的Shader下的Alpha值,实现Cube若隐若现的效果。

    private float rotaSpeed = 5f; private float timer = 1; private bool flag = true; private float delay ...

  10. CSS(二)属性--文本设置

    HTML代码一 <body> <div>这是一个很黑很黑的夜晚,黑云密布,没有任何光亮透过.卖火柴的小姑娘.......</div> </body> C ...