Skip to content

Commit 4aecda7

Browse files
update 283 again
1 parent d68ffbe commit 4aecda7

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

  • src/main/java/com/fishercoder/solutions/firstthousand

src/main/java/com/fishercoder/solutions/firstthousand/_283.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ public static class Solution2 {
2222
public void moveZeroes(int[] nums) {
2323
// this solution is the most optimal since it minimizes the number of operations
2424
// 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;
25+
for (int insertPos = 0, curr = 0; curr < nums.length; curr++) {
26+
if (nums[curr] != 0) {
27+
int temp = nums[insertPos];
28+
nums[insertPos++] = nums[curr];
29+
nums[curr] = temp;
3230
}
3331
}
3432
}

0 commit comments

Comments
 (0)