-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathZeroMover.java
35 lines (26 loc) · 890 Bytes
/
ZeroMover.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
package two_pointers;
public class ZeroMover {
public void moveZeroes(int[] arr) {
int nonZeroIndex = 0;
for (int num : arr) {
if (num != 0) {
arr[nonZeroIndex] = num;
nonZeroIndex++;
}
}
while (nonZeroIndex < arr.length) {
arr[nonZeroIndex] = 0;
nonZeroIndex++;
}
}
public static void main(String[] args) {
ZeroMover mover = new ZeroMover();
int[] testInput1 = {0,1,0,3,12};
mover.moveZeroes(testInput1);
assert java.util.Arrays.equals(testInput1, new int[]{1,3,12,0,0}) : "Test case 1 failed";
int[] testInput2 = {0};
mover.moveZeroes(testInput2);
assert java.util.Arrays.equals(testInput2, new int[]{0}) : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}