-------------------------AC JAVA O(n)---------------------------


  • 0
    M

    public class Solution {

    public void moveZeroes(int[] nums) {
    
        int k=0;
        int len=nums.length;
        for(int i=0;i+k<len;)
        {
            if(k!=0)
            {
                nums[i]=nums[i+k];
            }
            if(nums[i]==0)
            {
                k++;
            }
            if(nums[i]!=0)
            {
                i++;
            }
        }
        if(k!=0)
        {
            for(int i=len-k;i<len;i++)
            {
                nums[i]=0;
            }
        }
    }
    

    }


Log in to reply
 

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