Java concise O(n) solution


  • 0
    Y

    concise java O(n) solution

    public class Solution {
        public void moveZeroes(int[] nums) {
            for(int i=0;i<nums.length;i++)
            {
                if(nums[i]!=0)
                {
                    continue;
                }
                else{
                    for(int j=i+1;j<nums.length;j++)
                    {
                        if(nums[j]!=0)
                        {
                            change(nums,i,j);
                            //so next the point we will look at will be i=j but since i will be increased, that is why we minus 1 here.
                            j=i-1;
                            break;
                        }
                    }
                }
            
            }
               
        }
        
        public void change(int[] nums, int i, int j)
        {
            int temp=0;
            temp=nums[i];
            nums[i]=nums[j];
            nums[j]=temp;
        }
    }
    

  • 0
    A

    @yliang "O(n) solution" - ?


Log in to reply
 

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