-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovezeros.java
41 lines (39 loc) · 1.03 KB
/
movezeros.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
public class movezeros {
public static void main(String[] args) {
int[] arr = { 0, 1, 0, 3, 12 };
int[] sec = move(arr);
for (int num : sec) {
System.out.print(num);
}
}
public static int[] move(int[] arr) {
// int firstzeroind = 0;
// for (int i = 0; i < arr.length; i++) {
// if (arr[i] == 0) {
// firstzeroind = i;
// break;
// }
// }
// for (int i = firstzeroind; i < arr.length - 1; i++) {
// if (arr[i + 1] != 0) {
// while (arr[i + 1] != 0) {
// int temp = arr[i + 1];
// arr[i + 1] = arr[i];
// arr[i] = temp;
// }
// } else {
// continue;
// }
// }
int nonzero = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
int temp = arr[i];
arr[i] = arr[nonzero];
arr[nonzero] = temp;
nonzero++;
}
}
return arr;
}
}