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. Android的移动存储解决方案之SharedPreferences

    搞Android有一段时间了,但是并没写过有关Android的博客,今天给大家介绍一下SharedPreferences.    使用SharedPreferences保存key-value对的步骤如 ...

  2. java数组遍历 删除remove

    package com.b; import java.util.ArrayList; //数组遍历删除,添加 public class Core2 { private String name; pri ...

  3. MYSQL数据库索引类型都有哪些?

    索引类型: B-TREE索引,哈希索引•B-TREE索引加速了数据访问,因为存储引擎不会扫描整个表得到需要的数据.相反,它从根节点开始.根节点保存了指向子节点的指针,并且存储引擎会根据指针寻找数据.它 ...

  4. Vue.js:条件与循环

    ylbtech-Vue.js:条件与循环 1.返回顶部 1. Vue.js 条件与循环 条件判断 v-if 条件判断使用 v-if 指令: v-if 指令 在元素 和 template 中使用 v-i ...

  5. 微信小程序只之全局存储

    全局变量 app.globalData 本地缓存 wx.setStorageSync(KEY,DATA) wx.getStorageSync(KEY) wx.getStorageInfoSync wx ...

  6. SE新手游操控创新:一个按键=五个技能

    转自:http://www.gamelook.com.cn/2015/01/201299 GameLook报道 / 日本游戏厂商一向擅长搞发明创造,除了诞生了各种烧脑奇葩游戏以外,日本主流手机游戏的核 ...

  7. Java标准I/O流介绍

    1.I/O是什么? I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 2.流 流是一个连续的数据流,可以从流中读取数据,也可以往流中写数据.流与 ...

  8. vsftpd设置被动模式

    完整配置 listen=yes listen_port= max_clients= max_per_ip= local_max_rate= anonymous_enable=no local_enab ...

  9. 爬取github上流行的python项目

    # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import requests from pyquery import PyQ ...

  10. python 模块 optparse

    optparse,是一个能够让程式设计人员轻松设计出简单明了.易于使用.符合标准的Unix命令列程式的Python模块.生成使用和帮助信息. 下面是一个简单的示例: import optparse p ...