Skip to content

Commit 598c7b5

Browse files
committed
Q283 - Move Zeroes solution added
1 parent 82137e2 commit 598c7b5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

java/src/easy/Q283_MoveZeroes.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package src.easy;
2+
3+
import java.util.Arrays;
4+
5+
public class Q283_MoveZeroes {
6+
public static void main(String[] args) {
7+
int[] nums = {0, 1, 0, 3, 12};
8+
moveZeroes(nums);
9+
Arrays.stream(nums).forEach(System.out::println); // 1,3,12,0,0
10+
}
11+
12+
13+
public static void moveZeroes(int[] nums) {
14+
int insertPosition = 0;
15+
for (int num : nums) {
16+
if (num != 0) {
17+
nums[insertPosition++] = num;
18+
}
19+
}
20+
while (insertPosition < nums.length) {
21+
nums[insertPosition++] = 0;
22+
}
23+
}
24+
}
25+

0 commit comments

Comments
 (0)