Java O(n)


  • 0
    A
    public class Solution {
        public void moveZeroes(int[] nums) {
            int j = -1;
            
            for (int i = 0; i<nums.length; i++){
                if (nums[i]==0){ if (j==-1) {j=i;} }
                else if (j!=-1) {
                        nums[j] = nums[i];
                        nums[i] = 0;
                        j++;
                }
            }
        }
    }

Log in to reply
 

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