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.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [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.

题目大意:给定一个数组,把所有的0放到数组后面,所有的非0元素放到前面,并且保证非0元素顺序不变。

解题思路,从前往后遍历,遇见0则cnt++,遇见非0则往前移cnt个,最后把后cnt个设置为0就可以了。

    public void moveZeroes(int[] nums) {
if(nums==null||nums.length==0){
return;
}
int cnt = 0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
cnt++;
}else{
nums[i-cnt]=nums[i];
}
}
if(cnt>0){
Arrays.fill(nums,nums.length-cnt,nums.length,0);
}
}

Move Zeroes——Leetcode的更多相关文章

  1. 283. Move Zeroes - LeetCode

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

  2. LeetCode:Move Zeroes

    LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...

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

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

  4. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  5. 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 ...

  6. 【leetcode】283. Move Zeroes

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

  7. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

  8. 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 ...

  9. [LintCode] Move Zeroes 移动零

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

随机推荐

  1. CI框架篇之预热篇(1)

    CodeIgniter 的基本都了解了,现在就开始预热,如果学习一门语言一样,我们最开始都是输出一个'HELLO WORLD'一样, 现在我们也通过输出这样一个内容,来了解基本的使用. CodeIgn ...

  2. 牛皮市和猴市的好工具和指标:BOLL

    (转贴)布林线BOLL用法 布林线是股市中经常用到的技术指标之一,它反映了股价的波动状况.山版软件指标图中的布林线由三条组成,上边的白线(up)是阻力线,下边的黄线(down)是支撑线,中间的粉红线( ...

  3. RABBITMQ安装注意点

    关于 RABBITMQ的配置问题安装问题windows7 和window 10我都试了windows10安装和配置不要出现中文和空格,不然你日寒飞的心都有了ERLANG的安装也是Win7直接默认的路径 ...

  4. SQL分页语句总结

    今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了. 使用top选项 selecttop10*from Orders a where a.orderid notin(s ...

  5. swift 关于闭包和函数

    调用函数,有闭包参数时: 函数的实现中:闭包为参数时,有参数返回值类型: 调用闭包时,传入参数 调用函数时:闭包为参数,是闭包的实现,当闭包为最后一个参数时,可写在参数括号外面 即===>函数在 ...

  6. 原生Javascript实现控制DIV属性

    写在前面: 从事前端工作已有一年之久,因为工作的性质,不太涉及JS方面,所以自己的JS水平一直处于小白阶段,工作闲暇之余,在网上找了一些小项目,希望练练手,促进自己成长.这是第一篇,后续还会有很多记录 ...

  7. ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor

    Description Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way ...

  8. [学习笔记]设计模式之Facade

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Facade(外观)模式定义了一个高层接口,它能为子系统中的一组接口提供一个一致的界面,从而使得这一子系统更加容易使用.欢迎回到时の魔 ...

  9. Windows phone 之常用控件

    一.TextBox TextBox 显示和编辑单格式.多行文本的控件 将TextWrapping的特性设置为Wrap会使文本在到达TextBox控件的边缘时换至新行.必要时会自动扩展TextBox以便 ...

  10. PHP IP互换数字[转]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...