350 Intersection of Two Arrays II

1. Question

Given two arrays, write a function to compute their intersection.

Example: Givennums1=[1, 2, 2, 1],nums2=[2, 2], return[2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.

  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?

  • What if nums1's size is small compared to nums2's size? Which algorithm is better?

  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

2. Implementation

(1) Two Pointers

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        List<Integer> list = new ArrayList<>();

        int i = 0, j = 0;

        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] < nums2[j]) {
                ++i;
            }
            else if (nums1[i] > nums2[j]) {
                ++j;
            }
            else {
                list.add(nums1[i]);
                ++i;
                ++j;
            }
        }

        int[] res = new int[list.size()];
        int index = 0;

        for (int e : list) {
            res[index++] = e;
        }
        return res;
    }
}

(2) Binary Search

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return new int[0];
        }

        if (nums1.length < nums2.length) {
            return getIntersection(nums1, nums2);
        }
        else {
            return getIntersection(nums2, nums1);
        }
    }

    public int[] getIntersection(int[] nums1, int[] nums2) {
        List<Integer> list = new ArrayList<>();
        List<Integer> intersection = new ArrayList<>();

        Arrays.sort(nums2);

        for (int num : nums2) {
            list.add(num);
        }

        for (int i = 0; i < nums1.length; i++) {
            int searchIndex = binarySearch(list, nums1[i]);

            if (searchIndex != -1) {
                intersection.add(nums1[i]);
                list.remove(searchIndex);
            }
        }

        int[] res = new int[intersection.size()];
        int index = 0;

        for (int e : intersection) {
            res[index++] = e;
        }
        return res;
    }

    public int binarySearch(List<Integer> list, int target) {
        int start = 0, end = list.size() - 1, mid = 0;

        while (start <= end) {
            mid = start + (end - start) / 2;

            if (list.get(mid) == target) {
                return mid;
            }
            else if (list.get(mid) < target) {
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
        return -1;
    }
}

3. Time & Space Complexity

Two Pointers: 时间复杂度O(nlogn + mlogm),n为nums1的长度, m为nums2的长度,空间复杂度O(Math.min(m,n))

Binary Search: 时间复杂度O(nlogn + mlogn), m为num1的长度, n为nums2的长度,空间复杂度O(Math.min(m,n))

Last updated