We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d68ffbe commit 4aecda7Copy full SHA for 4aecda7
1 file changed
src/main/java/com/fishercoder/solutions/firstthousand/_283.java
@@ -22,13 +22,11 @@ public static class Solution2 {
22
public void moveZeroes(int[] nums) {
23
// this solution is the most optimal since it minimizes the number of operations
24
// the idea is to swap the non-zero element to the first zero number position
25
- for (int nonZeroIndex = 0, zeroIndex = 0;
26
- nonZeroIndex < nums.length && zeroIndex < nums.length;
27
- zeroIndex++) {
28
- if (nums[zeroIndex] != 0) {
29
- int temp = nums[nonZeroIndex];
30
- nums[nonZeroIndex++] = nums[zeroIndex];
31
- nums[zeroIndex] = temp;
+ for (int insertPos = 0, curr = 0; curr < nums.length; curr++) {
+ if (nums[curr] != 0) {
+ int temp = nums[insertPos];
+ nums[insertPos++] = nums[curr];
+ nums[curr] = temp;
32
}
33
34
0 commit comments