-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoveZeroes.java
More file actions
46 lines (37 loc) · 1.34 KB
/
Copy pathMoveZeroes.java
File metadata and controls
46 lines (37 loc) · 1.34 KB
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
44
45
46
// 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.
// See: https://leetcode.com/problems/move-zeroes/
package leetcode.array_and_hashtable;
public class MoveZeroes {
public void moveZeroes(int[] nums) {
int lastNonZero = 0;
for (int i = 0; i < nums.length; i++)
if (nums[i] != 0) {
nums[lastNonZero] = nums[i];
lastNonZero++;
}
for (int i = lastNonZero; i < nums.length; i++)
nums[i] = 0;
// System.out.println(Arrays.toString(nums));
}
// Accepted but sub-optimal
public void moveZeroes1(int[] nums) {
int l = 0, r = 0;
while (r < nums.length && l < nums.length) {
while (l < nums.length && nums[l] != 0) l++;
r = l + 1;
while (r < nums.length && nums[r] == 0) r++;
if (l < nums.length && r < nums.length) {
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
}
}
public static void main(String[] args) {
MoveZeroes sln = new MoveZeroes();
sln.moveZeroes(new int[] { 0, 1, 0, 3, 12 });
sln.moveZeroes(new int[] { 1, 0, 0, 3, 12 });
sln.moveZeroes(new int[] { 1, 2, 0, 3 });
}
}