天天看點

求兩個升序數組的交集,并集,補集

通過兩個數組的下标比較,時間複雜度為O(n)

public class Set {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array1 = {, , , , , };
        int[] array2 = {, , , , , , };

        getIntersection(array1, array2);
        getUnion(array1, array2);
        getComplement(array1, array2);
    }

    /**
     * 求兩個數組的補集
     * @param array1
     * @param array2
     */
    public static void getComplement(int[] array1, int[] array2) {
        int index1 = ;
        int index2 = ;
        while(index1 < array1.length && index2 < array2.length) {
            if (array1[index1] == array2[index2]) {
                index1++;
                index2++;
            } else if (array1[index1] < array2[index2]) {
                System.out.println(array1[index1]);
                index1++;
            } else {
                System.out.println(array2[index2]);
                index2++;
            }

            if (index1 == array1.length) {
                while (index2 < array2.length) {
                    System.out.println(array2[index2]);
                    index2++;
                }
            }

            if (index2 == array2.length) {
                while (index1 < array1.length) {
                    System.out.println(array1[index1]);
                    index1++;
                }
            }
        }
        System.out.println("----------------------");
    }

    /**
     * 求兩個數組的并集
     * @param array1
     * @param array2
     */
    public static void getUnion(int[] array1, int[] array2) {
        int index1 = ;
        int index2 = ;
        while(index1 < array1.length && index2 < array2.length) {
            if (array1[index1] == array2[index2]) {
                System.out.println(array1[index1]);
                index1++;
                index2++;
            } else if (array1[index1] < array2[index2]) {
                System.out.println(array1[index1]);
                index1++;
            } else {
                System.out.println(array2[index2]);
                index2++;
            }

            if (index1 == array1.length) {
                while (index2 < array2.length) {
                    System.out.println(array2[index2]);
                    index2++;
                }
            }

            if (index2 == array2.length) {
                while (index1 < array1.length) {
                    System.out.println(array1[index1]);
                    index1++;
                }
            }
        }
        System.out.println("----------------------");
    }

    /**
     * 求兩個數組的交集
     * @param array1
     * @param array2
     */
    public static void getIntersection(int[] array1, int[] array2) {
        int index1 = ;
        int index2 = ;
        while(index1 < array1.length && index2 < array2.length) {
            if (array1[index1] == array2[index2]) {
                System.out.println(array1[index1]);
                index1++;
                index2++;
            } else if (array1[index1] < array2[index2]) {
                index1++;
            } else {
                index2++;
            }
        }
        System.out.println("----------------------");
    }
}