荷兰国旗问题

209 阅读2分钟

已知一个整型数组arr,和一个整数num,请把小于num的数放在数组的左边,等于num的数放在数组的中间,大于num的数放在数组的右边。

要求:时间复杂度为O(N),额外空间复杂度O(1)。

理解

1、设置 left 为左边界,right 为右边界,less 为小于区域右边界,more 为大于区域左边界,number为给定的值。

2、left从左向右移动:
若arr[left]<number,则将less向右移动,然后交换arr[less]和arr[left]的值,然后left向右移动。
若arr[left]>number,则将more向左移动,交换arr[more]和arr[left]的值

3、结束条件,只有arr[left]<number时,left向右侧移动,left不会和less重合。因此需要考虑右边界,右边界从右向左逐渐接近left,因此当left和more重合时,循环结束。

4、过程如下

代码

public class NetherlandsFlag {

    /**
     * @param arr    原始数组
     * @param left   左边界
     * @param right  右边界
     * @param number 指定的数字
     * @return 返回调整后的数组
     */
    public static int[] partition(int[] arr, int left, int right, int number) {
        int less = left - 1;//less 为小于区域右边界
        int more = right + 1;//more 为大于区域左边界
        //从左边开始遍历 more在减少,left不能和more重合
        while (left < more) {
            if (arr[left] < number) {
                swapArrayElement(arr, ++less, left++);
            } else if (arr[left] > number) {
                swapArrayElement(arr, --more, left);
            } else {
                left++;
            }
            System.out.println("less="+less+",left="+left+",more="+more+",right="+right);
            printArray(arr);

        }

        return arr;
    }


    public static void swapArrayElement(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }


    /**
     * 生成数组
     *
     * @return
     */
    public static int[] generateArray() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 3);
        }
        return arr;
    }


    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] array = {2, 5, 4, 7, 3, 8, 1};
        int[] res = partition(array, 0, array.length - 1, 5);
    }
}