Description:

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.

时空复杂度都在O(n)内

  1. public class Solution {
  2. public void moveZeroes(int[] nums) {
  3.  
  4. //nums = [0, 1, 0, 3, 12]->[1, 3, 12, 0, 0]
  5. for(int i=0,j=0; i<nums.length; i++) {
  6. if(nums[i]!=0) {
  7. int t = nums[i];
  8. nums[i] = nums[j];
  9. nums[j] = t;
  10. j ++;
  11. }
  12. }
  13.  
  14. }
  15. }

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

  1. LeetCode:Move Zeroes

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

  2. [LeetCode] Move Zeroes 移动零

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

  3. LeetCode Move Zeroes (简单题)

    题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...

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

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

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

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

  7. 【leetcode】283. Move Zeroes

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

  8. 【leetcode】Move Zeroes

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

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

随机推荐

  1. CKFinder 自定义文件路径扩展ConfigurationPathBuilder

    CKFinder 自定义文件路径扩展ConfigurationPathBuilder 打开config.xml当中可以看到如下配置 <basePathBuilderImpl>com.ckf ...

  2. MongoDB 集群搭建(主从复制、副本及)(五)

    六:架构管理 mongodb的主从集群分为两种: 1:master-Slave 复制(主从)    --从server不会主动变成主server,须要设置才行 2:replica Sets 复制(副本 ...

  3. java-javaweb_URL重写

    Java WEB实现URL重写的优缺点及如何实现: http://blog.csdn.net/cselmu9/article/details/8062033 urlrewrite 地址重写: http ...

  4. js学习笔记21----表格操作

    1.获取表格元素: tHead : 表格头 tBody : 表格主体内容 tFoot : 表格尾 rows  : 表格行 cells  : 表格列 如获取表格第一行第一列的数据: <script ...

  5. jQuery EasyUI教程之datagrid应用-1

    一.利用jQuery EasyUI的DataGrid创建CRUD应用 对网页应用程序来说,正确采集和管理数据通常很有必要,DataGrid的CRUD功能允许我们创建页面来列表显示和编辑数据库记录.本教 ...

  6. VMware提示:已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。解决方案

    新买了个笔记本,在学习大数据的时候装上VMWare,运行虚拟机发现提示无法执行64位操作.本人系统是win7,64位系统. 之后就是一顿度娘,发现千篇一律都是检测CPU支不支持虚拟化,支持的话去BIO ...

  7. 获得android手机的联网状态

    获得android手机的联网状态   在Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况.下面的代码,作为例子,详细说明了对于当前网络情况的判断. 先看一个自己定义的应用类. ...

  8. C# 窗体显示避免抢夺焦点

    通过调用API进行显示可以避免抢夺焦点的问题 以下是API调用 using System.Runtime.InteropServices; [DllImport("user32.dll&qu ...

  9. 第三百二十节,Django框架,生成二维码

    第三百二十节,Django框架,生成二维码 用Python来生成二维码,需要qrcode模块,qrcode模块依赖Image 模块,所以首先安装这两个模块 生成二维码保存图片在本地 import qr ...

  10. page指令属性简要介绍:

    page指令属性简要介绍: language=”java” 声明脚本语言的种类,暂时只能用”java” extends=”package.class” 标明JSP编译时需要加入的Java Class的 ...