【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: ...
随机推荐
- POJ1742:Coins
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:http://poj.org/problem?id=1742 多重背包,每个物 ...
- Linux cc与gcc
三个源文件:main.c sum.c show.c /* main.c */ extern int sum(int m, int n); extern void show(int data); int ...
- Centos7手工安装Kubernetes集群
安装Kubernetes集群有多种方式,前面介绍了Kubeadm的方式,本文将介绍手工安装的方法. 安装环境有3台Azure上的VM: Hkube01:10.0.1.4 Hkube02:10.0.1. ...
- 关于bc 的scale .
linux下的bc命令可以设置结果的位数,通过 scale. 比如: $ echo "scale=4; 1.2323293128 / 1.1" | bc -l1.1202 但是sc ...
- MyBatis 一对一(OneToOne)__SELECT
1.创建SQL脚本: CREATE TABLE t_person( id int(3) not null auto_increment, name varchar(20) default null ...
- 杂项:WiKi
ylbtech-杂项:WiKi Wiki是一种在网络上开放且可供多人协同创作的超文本系统,由沃德·坎宁安于1995年首先开发,这种超文本系统支持面向社群的协作式写作,同时也包括一组支持这种写作.沃德· ...
- UE4动画功能整理
转自:http://blog.ch-wind.com/ue4%E5%8A%A8%E7%94%BB%E5%8A%9F%E8%83%BD%E6%95%B4%E7%90%86/ 工欲善其事必先利其器,最近一 ...
- Access 数据库的数据类型
今天开发数据库通用组件时,做C#数据库类型与Access数据库类型的相互转化时,发现Access中“text”类型的最大长度为255,当长度超过255时,需改用“Memo”类型,但需要注意备注(Mem ...
- 第十二章 Java内存模型与线程(待续)
············
- cpu,io密集型计算概念
I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...