【leetcode】Move Zeroes
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的更多相关文章
- 【5_283】Move Zeroes
终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【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 ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
随机推荐
- WARNING: at drivers/gpio/gpiolib.c:101 gpio_ensure_requested+0x5c/0x118()
使用输入子系统实现的按键程序,每次按键后进入中断,就会报错如下: ---input_key_handler--- ------------[ cut here ]------------ WARNIN ...
- (转)Oracle 包(Package)
本文转载自:http://www.cnblogs.com/lovemoon714/archive/2012/02/29/2373695.html 1.为什么要使用包? 答:在一个大型项目中 ...
- 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...
- Redis存储AccessToken
AccessToken 2小时有效. 就不要每次都调取了,这样会造成浪费. 或者存入Session中,设置过期时间. 或者存入Redis中,设置过期时间. 过期之后,进行重新获取. <?php ...
- python开发函数进阶:装饰器
一,装饰器本质 闭包函数 功能:就是在不改变原函数调用方式的情况下,在这个函数前后加上扩展功能 作用:解耦,尽量的让代码分离,小功能之前的分离. 解耦目的,提高代码的重用性 二,设计模式 开放封闭原则 ...
- apk、图片下载工具(1)
package com.js.ai.modules.pointwall.util; import java.io.BufferedInputStream; import java.io.Buffere ...
- C Primer Plus学习笔记(九)- 数组和指针
数组 数组由数据类型相同的同一系列元素组成 需要使用数组时,通过声明数组告诉编译器数组中内含多少元素和这些元素的类型 普通变量可以使用的类型,数组元素都可以用 float candy[365]; // ...
- MFC 菜单栏杂记
1.关于为毛要使用detach()函数 CMenu menu; menu.LoadMenu(IDR_MAINFRAME); SetMenu(&menu); menu.Detach(); //如 ...
- ngnix 403 forbidden的解决办法
1.在网站根目录下新建文件index.html.index.php. 2.主要是修改nginx的配置文件nginx.conf权限为755即可访问.
- leetcode762
class Solution { public: bool IsPrime(int n) { ) { return false; } || n == ) { return true; } ; i &l ...