# Easy Java solution O(N^2) any optimization?

• Any optimization to make it O(n) time complexity please?

``````public class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
for (int i = 0; i < nums.length; i++) {
for (int j = i+1; j < nums.length; j++) {
if (nums[i] == 0 && nums[j] != 0){
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
i++;
}
}
}
}
}
``````

• @sgoogs

int len = nums.length;
int count = 0;
int tempCount = 0;

for (; count < len; count ++){
if ( nums [count] == 0){
tempCount ++;
}

if (nuts[count] != 0){
nums[count-tempCount] = num [count];
}

}

int rest = len - tempCount;

while ( rest < len){
mums[rest] = 0;
}

}

• @sgoogs

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

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