Home > Net >  Program exits loop, can't understand why even with debugging
Program exits loop, can't understand why even with debugging

Time:01-14

It fails at the example [0,0,1], the output is [0,1,0] instead of [1,0,0]. I have gone through the code in debugging mode and when p1 becomes 0, it jumps out of the for loop and I don't understand why, because it should go through the loop one more time and then decrement to -1 and exit the loop.

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.

Example 1:

Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]

Example 2:

Input: nums = [0] Output: [0]

package random.leetCode;

import java.util.Arrays;

public class MoveZeroes_TwoPointers_283 {
    public void moveZeroes(int[] nums) {
        int p2 = 0;
        int tempIndex = 0;
        int lastIndex = nums.length - 1;

        for (int p1 = nums.length - 1; p1>=0;  p1--)
        {
            if (nums[p1] == 0)
            {
                if (p1 == nums.length - 1 && nums.length == 1)
                {
                    break;
                }

                tempIndex = p1;
                p2 = p1   1;
                while (p2 <= lastIndex)
                {
                    int temp = nums[p1];
                    nums[p1] = nums[p2];
                    nums[p2] = temp;
                    p2  = 1;
                    p1  = 1;
                }
                p1 = tempIndex -1;
                lastIndex--;
            }
        }
    }

    public static void main(String[] args) {
        MoveZeroes_TwoPointers_283 example = new MoveZeroes_TwoPointers_283();
//       int[] numbersToBeSorted = new int[]{0,1,0,3,12,0,11,0,0};
//        int[] numbersToBeSorted = new int[]{0};
//        int[] numbersToBeSorted = new int[]{0,0};
//        int[] numbersToBeSorted = new int[]{1};
//        int[] numbersToBeSorted = new int[]{1,1};
       int[] numbersToBeSorted = new int[]{0,0,1};
        example.moveZeroes(numbersToBeSorted);
        System.out.println(Arrays.toString(numbersToBeSorted));
    }
}```

CodePudding user response:

for (int p1 = nums.length - 1; p1>=0;  p1--) {
...
    tempIndex = p1;
...
    p1 = tempIndex -1;
}

You are decrementing p1 twice in this loop. You probably did not mean to.

CodePudding user response:

Interesting. I think the problem is when you find two consecutive zeros and managing these pointers. Let me know what you think about this algorithm.

void main() {
    int[] input = new int[]{0,0,1};
    
    // whenever finds a zero, move it to the end
    for (int i=0; i < input.length; i  ) {
       if (input[i] == 0) moveToEnd(i, input);
    }

    System.out.println(input);
}

// move to the end by swapping elements
void moveToEnd(pos, arr) {
   for (int i=pos; i < arr.length; i  ) {
       int aux = arr[pos];
       arr[pos] = arr[i];
       arr[i] = aux;
   }
}
  •  Tags:  
  • Related