@@ -0,0 +1,2856 @@
+package easy;%0A%0Aimport utils.CommonUtils;%0A%0A/**283. Move Zeroes  QuestionEditorial Solution  My Submissions%0ATotal Accepted: 105705%0ATotal Submissions: 231420%0ADifficulty: Easy%0AGiven an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.%0A%0AFor example, given nums = %5B0, 1, 0, 3, 12%5D, after calling your function, nums should be %5B1, 3, 12, 0, 0%5D.%0A%0ANote:%0AYou must do this in-place without making a copy of the array.%0AMinimize the total number of operations.*/%0Apublic class MoveZeroes %7B%0A    public void moveZeroes_Editorial_solution2(int%5B%5D nums)%7B%0A        //this solutoin is the most optimal since it minimizes the number of operations%0A        //the idea is to swap the non-zero element to the first zero number position%0A        for(int i = 0, j = 0; i %3C nums.length && j %3C nums.length; i++)%7B%0A            if(nums%5Bi%5D != 0)%7B%0A                int temp = nums%5Bi%5D;%0A                nums%5Bi%5D = nums%5Bj%5D;%0A                nums%5Bj%5D = temp;%0A                j++;%0A            %7D%0A        %7D%0A    %7D%0A    %0A    public void moveZeroes_Editorial_solution1(int%5B%5D nums)%7B%0A        //keep the last non-zero index and keep overwriting it, then append zeroes to fill the end%0A        int j = 0, i = 0;%0A        for(; j %3C nums.length; j++)%7B%0A            if(nums%5Bj%5D != 0)%7B%0A                nums%5Bi++%5D = nums%5Bj%5D;%0A            %7D%0A        %7D%0A        for(; i %3C nums.length; i++)%7B%0A            nums%5Bi%5D = 0;%0A        %7D%0A    %7D%0A    %0A    //then I came up with this solution and got it AC'ed! Cheers!%0A    //basically, find the next non-zero number and swap it with the current zero number%0A    //Apparently it's not the most optimal, since this is basically an O(n%5E2) solution, then I turned to Editorial solutions%0A    public void moveZeroes(int%5B%5D nums)%7B%0A        for(int i = 0; i %3C nums.length-1; i++)%7B%0A            if(nums%5Bi%5D == 0)%7B%0A                int j = i+1;%0A                while(j %3C nums.length && nums%5Bj%5D == 0)%7B%0A                    j++;%0A                %7D%0A                if(j %3E= nums.length) return;%0A                else %7B%0A                    int temp = nums%5Bj%5D;%0A                    nums%5Bj%5D = nums%5Bi%5D;%0A                    nums%5Bi%5D = temp;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    //this approach won't preserve the relative order of the non-zero numbers%0A    public void moveZeroes_1st_attempt(int%5B%5D nums) %7B%0A        int i = 0, j = nums.length-1;%0A        while(i %3C j)%7B%0A            if(nums%5Bi%5D == 0)%7B%0A                int temp = nums%5Bj%5D;%0A                nums%5Bj%5D = nums%5Bi%5D;%0A                nums%5Bi%5D = temp;%0A                j--;%0A            %7D else %7B%0A                i++;%0A            %7D%0A        %7D%0A        CommonUtils.printArray(nums);%0A    %7D%0A    %0A    public static void main(String...strings)%7B%0A        MoveZeroes test = new MoveZeroes();%0A        int%5B%5D nums = new int%5B%5D%7B0,1,0,3,12%7D;%0A        test.moveZeroes_Editorial_solution2(nums);%0A    %7D%0A%7D%0A

move zeroes