-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-283-Move-Zeroes.java
43 lines (34 loc) · 1.23 KB
/
LeetCode-283-Move-Zeroes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
LeetCode: https://leetcode.com/problems/move-zeroes/
LintCode: not
JiuZhang: not
ProgramCreek: not
GFG: http://www.geeksforgeeks.org/move-zeroes-end-array/
Analysis:
*/
public class Solution {
// public void moveZeroes(int[] nums) {
// if(nums == null || nums.length == 0) return;
// int zero = 0;
// int nonZero = 0;
// while(zero < nums.length && nonZero < nums.length){
// while(zero < nums.length && nums[zero] != 0) zero++;
// nonZero = zero;
// while(nonZero < nums.length && nums[nonZero] == 0) nonZero++;
// if(zero > nonZero || zero >= nums.length || nonZero >= nums.length) break;
// int temp = nums[zero];
// nums[zero] = nums[nonZero];
// nums[nonZero] = temp;
// zero++;
// nonZero++;
// }
// }
public void moveZeroes(int[] nums) {
if(nums == null || nums.length == 0) return;
int count = 0; // index of non-zero element
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0) nums[count++] = nums[i];
}
while(count < nums.length) nums[count++] = 0;
}
}