Just one loop and need not move few parts of position.


  • -1
    W

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

    }


  • -1
    W

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

    }


Log in to reply
 

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