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就往后移,一直移动最后一步。代码如下

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

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

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

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

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

这样就解决了

其他方法

  1. public static void moveZeroes(int[] nums) {
  2. int i = 0, j = 0;
  3. for (i = 0; i < nums.length; i++) {
  4. if (nums[i] != 0) {
  5. if (j != i) {
  6. nums[j] = nums[i];
  7. nums[j] = 0;
  8. }
  9. j++;
  10. }
  11. }
  12. }

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

还有一种更简单的方法

  1. public void moveZeroes(int[] nums) {
  2. if (nums == null || nums.length == 0) return;
  3. int insertPos = 0;
  4. for (int num: nums) {
  5. if (num != 0) nums[insertPos++] = num;
  6. }
  7. while (insertPos < nums.length) {
  8. nums[insertPos++] = 0;
  9. }
  10. }

【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. java md5 函数

    private static final String md5(final String s) { final String MD5 = "MD5"; try { // Creat ...

  2. POJ2478(欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15242   Accepted: 6054 D ...

  3. (转)Oracle执行字符串

    declare v_out ); begin execute immediate 'select p_guid from c_itcomp where rownum = 1 ' into v_out; ...

  4. Vue.js:路由

    ylbtech-Vue.js:路由 1.返回顶部 1. Vue.js 路由 本章节我们将为大家介绍 Vue.js 路由. Vue.js 路由允许我们通过不同的 URL 访问不同的内容. 通过 Vue. ...

  5. 阿里云openapi接口使用,PHP,视频直播

    1.下载sdk放入项目文件夹中 核心就是aliyun-php-sdk-core,它的配置文件会自动加载相应的类 2.引入文件 include_once LIB_PATH . 'ORG/aliyun-o ...

  6. python开发函数进阶:递归函数

    一,什么叫递归 #递归#在一个函数里调用自己#python递归最大层数限制 997#最大层数限制是python默认的,可以做修改#但是我们不建议你修改 例子和尚讲故事 #!/usr/bin/env p ...

  7. Maria数据库

    项目上要进行数据库选型,业务上来讲,数据是非常结构化的数据,使用传统关系数据库更适合:另外项目采用微服务框架,每个服务的数据库应该尽可能轻量级, 最后考虑Maria数据库. MariaDB简介: Ma ...

  8. jgrid相关功能用法记录

    1.获取行号var ids = $gridList.jqGrid('getGridParam', 'selarrrow'); //多选,返回选中行号组字符 var ids2 = $gridList.j ...

  9. maven项目如何启动运行---发布到tomcat中

    前面两篇文章: 新建maven框架的web项目 以及 将原有项目改成maven框架 之后,我们已经有了maven的项目 那么 maven项目到底怎么启动呢 如果我们直接在myeclipse中按以前的启 ...

  10. 「小程序JAVA实战」微信小程序简介(一)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-01/ 一直想学习小程序,苦于比较忙,加班比较多没时间,其实这都是理由,很多时候习惯了搬砖,习惯了固 ...