# 1ms Java solution

• public class Solution {

``````public void moveZeroes(int[] nums) {

int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
j++;
}
}
}
``````

}

• NICE,GOOD JOB!

• Thank you....

• Oh my God. This idea is so concise. I have submitted a stupid solution.

• That happened to me like a 100 times. But that is the point in sharing answers; you keep improving.
I think someone might be able to do it without even using the temp variable because nums[j] is always supposed to be 0. If you want to give it a shot....

• Inspired by your code, my implementation doesn't use a temp variable and avoids unnecessary swaps when nums has leading non-zero element.

Idea - Set leftMostZeroIndex to 0. Iterate through the array, at each iteration i, if nums[i] != 0 and i > leftMostZeroIndex, replace the leftmost zero element nums[leftMostZeroIndex] with nums[i], and set nums[i] to 0.

Note that i >= leftMostZeroIndex is always true, and i == leftMostZeroIndex happens when nums has leading non-zero elements, e.g., nums = {2, 1, 3, 0, 5, 0, 6}. In such a case, we don't perform any swap, and keep incrementing i and leftMostZeroIndex until i > leftMostZeroIndex.

``````public void moveZeroes(int[] nums) {
int leftMostZeroIndex = 0; // The index of the leftmost zero
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
if (i > leftMostZeroIndex) { // There are zero(s) on the left side of the current non-zero number, swap!
nums[leftMostZeroIndex] = nums[i];
nums[i] = 0;
}

leftMostZeroIndex++;
}
}
}``````

• @Walkboy: Very elegant solution! Thanks!

• I just thought of this after reading the first solution. Why to store zeros in a temp value and shifting them! Brilliant!! :)

• this should be moved to the top.

• This is a brilliant solution that not only applies for moving zeros, but also moving other numbers.
eg. move any number smaller than 5 to the end, keeping other elements in relative order.
[1, 2, 3, 4,5,6,7,8,9,10]

After using this algorithm, it goes to [5,6,7,8,9,10,1,2,3,4], which also keeps relative order of numbers.

• This post is deleted!

• @vshldc Got something similar :)

``````public void moveZeroes(int[] nums) {
for (int i = 0, j = 0; j < nums.length; j++)
if (nums[j] != 0)
swap(nums, i++, j);
}

private void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
``````

• ``````public void moveZeroes(int[] nums) {
int j=0;
for(int i=0;i<nums.length;i++){
if(nums[i] != 0){
if(i!=j){
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
j++;
}
}
}
``````

I just added one if condition to reduce the number of swap operations

• very good solution!!!

• @vshldc well,good solution,i konw what you mean:)

• Eliminates unnecessary swaps if array has no zero in it:

``````    public void moveZeroes(int[] nums) {
int toBeReplaced = -1;
for (int i = 0; i < nums.length;i++) {
if (nums[i] == 0) {
if (toBeReplaced == -1) {
toBeReplaced = i;
}
} else if (toBeReplaced != -1) {
nums[toBeReplaced] = nums[i];
nums[i] = 0;
toBeReplaced++;
}
}
}
}
`````````

• Simple bubble sort idea, good!

• My solution is similar to yours.

public class Solution {
public void moveZeroes(int[] nums) {
int j =0;
for(int index =0; index<nums.length; index++){
if(nums[index]!=0){
if(nums[j]==0){
int tmp =nums[index];
nums[index] = nums[j];
nums[j] = tmp;
}
j++;
}
}

``````}
``````

}

• @vshldc Good job!!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.