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. JavaScript实现多栏目切换效果

    效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content-Type" ...

  2. MSSQL生成整个数据库的SQL脚本的工具 scptxfr.exe

    scptxfr.exe的路径要正确declare @cMd varchar(1000)set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Micros ...

  3. For each db / table

    use master go exec master..sp_MSforeachdb 'use [?]; IF (SELECT db_id(''?'')) > 4 and (SELECT DATA ...

  4. js获取当前url参数

    //抓取url参数 function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theReque ...

  5. OC调用Swift 整理步骤!总结别人的!方便自己查找!

    1. 2. 上面的修改了一个配置项,有一个Product Module Name在后面会使用. 在工程里面点击File/New/File…,选择iOS/Source/Cocoa Touch Class ...

  6. [Lua]cocos framework

    package_support function cc.register(name, package) function cc.load(...) function cc.bind(target, . ...

  7. [Neural Networks] Momentum

    一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...

  8. cmd 进入不同的驱动盘及上下级目录

    “开始”=>“运行”,输入”cmd“,此时进入的是系统管理员的等待命令 如果想进入相应的盘符,如 d 盘,则输入 cd d:\,然后再次输入 d: 即可进入 d: 盘,输入两次相当于第二在是询问 ...

  9. 图片延迟加载插件jquery.lazyload.js的使用方法

    最新版的jquery.lazyload.js已不再是伪的延迟加载了 一.请按照基本使用方法说明设置 //载入JavaScript 文件 <script src="jquery.js&q ...

  10. dotnet core 开发体验之Routing

    开始 回顾上一篇文章:dotnet core开发体验之开始MVC 里面体验了一把mvc,然后我们知道了aspnet mvc是靠Routing来驱动起来的,所以感觉需要研究一下Routing是什么鬼. ...