Java solution using bubble sort


  • 0
    V
     public void moveZeroes(int[] nums) {
        for (int i = 0; i < nums.length-1; i++) {
            if (nums[i] == 0){
                switchzero(nums, i);
            }
        }
    }
    
    private void switchzero(int[] nums, int i) {
        for (int j = i+1;j<nums.length; j++) {
            if (nums[j] != 0){
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                i = j;
            }
        }
    }

Log in to reply
 

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