Move Zeroes

题目:

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:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.

一开始的想法是,遍历数组,遇到0就往后移,一直移动最后一步。代码如下

public class Solution {
public static void moveZeroes(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0 && i != (nums.length - 1)) {
for (int j = i; j < nums.length - 1; j++) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
}

但是,发现001这个测试用例不对。

恩,发现问题了,因为第一个数为0,第二个0就没办法移动到最后了。

换一种思路,不遍历找0,而是遍历找非0,遇到非0,且不在第一位,就往前移,直到它前面不是0为止。

public class Solution {
public static void moveZeroes(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0 && i != 0) {
for (int j = i; j > 0 && nums[j - 1] == 0; j--) {
int temp = nums[j - 1];
nums[j - 1] = nums[j];
nums[j] = temp;
}
}
}
}
}

这样就解决了

其他方法

public static void moveZeroes(int[] nums) {
int i = 0, j = 0;
for (i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
if (j != i) {
nums[j] = nums[i];
nums[j] = 0;
}
j++;
}
}
}

这种方法i遍历数组,只有当i指向为非0时,且ji不同时,j后移,将j处的数改成i处的数,并使i处的数字为0,ij同步后移,而当i指向为0时,只有i后移,j仍然指向0处。

还有一种更简单的方法

public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) return; int insertPos = 0;
for (int num: nums) {
if (num != 0) nums[insertPos++] = num;
} while (insertPos < nums.length) {
nums[insertPos++] = 0;
}
}

【leetcode】Move Zeroes的更多相关文章

  1. 【5_283】Move Zeroes

    终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...

  2. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  3. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  4. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  5. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  6. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  7. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

随机推荐

  1. tomcat部署war包启动后请求无响应,一直报404

    刚刚碰到一个坑,本地Eclipse启动了tomcat,调用接口就是404,一直怀疑是url问题,改来改去还是404.最后发现还真是url问题,问题不在接口路径,而是项目名.因为接口路径用的是RESTf ...

  2. python 安装相关命令-汇总

    Microsoft Windows [版本 10.0.14393] (c) 2016 Microsoft Corporation.保留所有权利. C:\Windows\system32>pyth ...

  3. Oracle中遇到的错误

    1. ORA-00937: 不是单组分组函数  和  不是group by表达式 --select count(corp_tn),state_code from t_oa_main where cor ...

  4. PTA 是否同一棵二叉搜索树(25 分)

    是否同一棵二叉搜索树(25 分) 给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到.例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始 ...

  5. 表空间 -- tablespace

    表空间是数据库的逻辑划分,一个表空间只能属于一个数据库.所有的数据库对象都存放在指定的表空间中.但主要存放的是表, 所以称作表空间. Oracle数据库中至少存在一个表空间,即SYSTEM的表空间. ...

  6. python开发mysql:表关系&单表简单查询

    一 一对多,多对一 1.1 建立多对一 ,一对多的关系需要注意 先建立被关联的表,被关联的字段必须保证时唯一的 在创建关联的表,关联的字段一定是可以重复的 1.2 示例: 出版社 多对一,多个老师可能 ...

  7. 用纯js实现一个文本编辑器

    效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. burpsuite扫描web目录

    1.进行抓包 2.将其发送到lntruder 3.使用替换脚本替换掉/ 4.替换 5.替换结果 6.将多余的$$删除,在/后面添加$$    //$$就是payload 7.测试结果  替换脚本代码: ...

  9. ClientDataSet1.LoadFromStream

    lStream3.Position := 0;        lDataSet.LoadFromStream(lStream3);

  10. Autofac 4 netcore with config demo

    Autofac 4+以上和以前的配置不一样的,最近在NetCore中做项目,得到实例时折腾了一番. 使用场景,用autofac实例化数据库类型,避免一个个的去new json配置文件: <?xm ...