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. ASP.NET MVC学习系列 WebAPI初探

    转自http://www.cnblogs.com/babycool/p/3922738.html 一.无参数Get请求 一般的get请求我们可以使用jquery提供的$.get() 或者$.ajax( ...

  2. Jquery Datatables(三)

    最近在项目中又使用Datatables的一个有趣功能,官网列子如下图: 点击“+”,展开列表,再次点击收缩. 官网的列子点击展开后的数据也是原来行中的数据,这边有了一个想法是否可以利用Ajax去动态加 ...

  3. 从腾讯QQ升级游戏之“快速加入游戏”功能的实现缺陷看C/S之间如何正确分配相关协作

    转载:http://space.itpub.net/17007506/viewspace-615570 笔者在闲暇时,偶尔会登录腾讯QQGame玩玩升级游戏.这确实是一款非常优秀的软件作品,腾讯的开发 ...

  4. SQL For Xml

    最近遇到点棘手的问题,大致如下: 1.数据局格式: 企业名称 排口名称 监测时间  监测因子 a b c    pH值 a b c   氨氮 a b c    化学需氧量(COD) 企业名称.排口名称 ...

  5. 关于shell脚本编程的10个最佳实践

    每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.但大家解决问题的方式却不尽相同,这要取决于对专业知识的掌握程度.使 用命令的种类.看待问题的方式等等.对于那些处在shell脚本 ...

  6. help python(查看模块帮助文档)

    查看模块帮助文档: help(len) -- docs for the built in len function (note here you type "len" not &q ...

  7. fedora23开发环境搭建手册

    chrome安装 [安装chrome教程] nodejs环境搭建 dnf install nodejs dnf install npm sublime text 编辑器安装配置 [fedora安装su ...

  8. ThinkPHP表单令牌验证功能详细介绍

    注:TP版本为3.1.3 在ThinkPHP框架下,两次提交同一个表单,比如提交信息后在浏览器点击后退退回上次的页面,重新点击提交按钮,就会提示“表单令牌错误”的信息. ThinkPHP新版内置了表单 ...

  9. Django 基础

    Django 的路由系统 在 django 的 URLconf 配置文件 urls.py 中根据一个 URL 对应 views 的一个函数来处理用户的请求. 1.基本的 urls 对应 urlpatt ...

  10. 2016021903 - 下载安装使用Memory Analyzer

    Memory Analyzer是做什么的? 分析java程序中分析内存泄露问题. 1.下载Memory Analyzer Memory Analyzer下载地址:http://www.eclipse. ...