- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveZeros.java
More file actions
Latest commit
25 lines (24 loc) · 709 Bytes
/
Copy pathMoveZeros.java
File metadata and controls
25 lines (24 loc) · 709 Bytes
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
publicclassMoveZeros {
publicint[] moveZeroes(int[] nums) {
intk = 0;
int[] arr = newint[nums.length];
for (inti = 0; i < nums.length; i++) {
if (nums[i] != 0)
arr[k++] = nums[i];
}
while (k < nums.length) {
arr[k++] = 0;
}
for (inti = 0; i < nums.length; i++)
nums[i] = arr[i];
returnnums;
}
publicstaticvoidmain(String[] args) {
int[] nums = { 0, 1, 0, 3, 12 };
MoveZerosobj = newMoveZeros();
int[] nums1 = obj.moveZeroes(nums);
for (inti = 0; i < nums.length; i++) {
System.out.print(nums1[i] + " ");
}
}
}