题目描述:

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.

解题思路:

将数组中的非零数字往前压缩,剩余的位置补零。

代码如下:

public class Solution {
public void moveZeroes(int[] nums) {
int pos = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
nums[pos++] = nums[i];
}
}
for(; pos < nums.length; pos++)
nums[pos] = 0;
}
}

  

Java [Leetcode 283]Move Zeroes的更多相关文章

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

  2. LeetCode 283. 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] 283. Move Zeroes 移动零

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

  4. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  5. Leetcode 283 Move Zeroes python

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

  6. 11. leetcode 283. Move Zeroes

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

  7. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  8. [leetcode]283. Move Zeroes移零

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

  9. Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)

    题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...

随机推荐

  1. 【转】perl中尖括号运算符(<>)使用说明

    perl中尖括号运算符的用途分享,这里简单介绍下,方便需要的朋友 perl中<>运算符可以有如下的用途: 1)如果尖括号中间是文件句柄,尖括号运算符允许你读取文件句柄,比如<STDI ...

  2. OOP三类继承的区别

    OOP继承的区别提纲: 1. 普通类继承,并非一定要重写父类方法.2. 抽象类继承,如果子类也是一个抽象类,并不要求一定重写父类方法.如果子类不是抽象类,则要求子类一定要实现父类中的抽象方法.3. 接 ...

  3. PHP 对数组数值进行排序,使用另一个容器

    <?php /* 排序方式::事实上只需要将要循环的数组进行N次循环,然后每次取最大的一个值*/ $array = array(100,25,10,258,33,48,10,5,13,58,33 ...

  4. pdf增加水印

    /// <summary> /// 为PDF添加水印或背景图片 /// </summary> /// <param name="strSourceFilePat ...

  5. .NET安装和配置Oracle数据访问组件(ODAC)

    Many ASP.NET applications access Oracle database for the data source. Oracle supports the .NET with ...

  6. css tricks

    http://css-tricks.com/styling-cross-browser-compatible-range-inputs-css/ http://css-tricks.com/brows ...

  7. bnuoj 27874 "Center" of [p]erimeter midpoints(计算几何)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=27874 [题意]: 给你一个三角形三个顶点的坐标ABC,三角形各边取一点DEF,将三角形周长平均分割 ...

  8. @Entity设置OneToMany

    Hibernate设置bean映射数据库的方式有配置模式与注解模式,下面通过注解模式配置OneToMany @Entity @Table(name="csdnbbs_sys_catalog& ...

  9. Codeforces Round #243 (Div. 2) A~C

    题目链接 A. Sereja and Mugs time limit per test:1 secondmemory limit per test:256 megabytesinput:standar ...

  10. linux Ubuntu12 设置root用户登录图形界面

    Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...