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)内

public class Solution {
public void moveZeroes(int[] nums) { //nums = [0, 1, 0, 3, 12]->[1, 3, 12, 0, 0]
for(int i=0,j=0; i<nums.length; i++) {
if(nums[i]!=0) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j ++;
}
} }
}

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. 恶补java基础 位运算符

    以前不知道在哪看过别人写博客里的一句话  “越学的深入觉得自己会的东西越少”.最近研究了一下java的基础算法  hash表 被一些位运算符搞的头都大了 决心花一些时间研究一下以前一直觉得没用的东西! ...

  2. JavaScrip——练习(做悬浮框再进一步:悬浮窗后缀悬浮窗——用this.className)

    对悬浮窗进一步改进: 用this.className 可以省略script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  3. Hive shell 命令

    Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...

  4. Userdata

    我们首先关心的是如何在Lua中表示数组的值.Lua为这种情况提供专门提供一个基本的类型:userdata.一个userdatum提供了一个在Lua中没有预定义操作的raw内存区域. Lua API提供 ...

  5. MapReduce 学习(一)

    首先我们先来欣赏一下MapReduce的执行过程吧,如下图,自己看,不解释了. Map 和 Reduce 的处理都是基于Key/Value来进行的,在Map中对文件的每一行进行处理,有两个输入参数,K ...

  6. 将string转换成char型的一般方法

    C++文件读取中: infile in: in.open("file.dat",ios::in); 这样是能够的. 可是 string a; a="file.dat&qu ...

  7. mongodb查询之从多种分类中获取各分类最新一条记录

    mongodb查询之从多种分类中获取各分类最新一条记录 2017年04月06日 13:02:47 monkey_four 阅读数:6707更多 个人分类: MongoDBJavaScript   文章 ...

  8. C#分割文件内容

    static void ReadData(string sourcePath, string targetDirectory) { FileStream fs = new FileStream(sou ...

  9. HTML坦克大战学习02---坦克动起来

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  10. 6款强大的 jQuery 网页布局创建及优化插件

    本文将为您介绍6款功能强大的jQuery插件,它们能够帮助您方便快捷地创建复杂的网络布局并进行优化. 1.UI.Layout 该插件可以创建任何你想要的UI形式:包括从简单的标题或侧边栏,到一个包含工 ...