[LeetCode] Intersection of Two Arrays I & II

320 查看

Intersection of Two Arrays I

Problem

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

Example

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note

Each element in the result must be unique.
The result can be in any order.

Solution

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map = new HashMap();
        List<Integer> res = new ArrayList();
        for (int i = 0; i < nums1.length; i++) {
            if (!map.containsKey(nums1[i])) map.put(nums1[i], 1);
            else map.put(nums1[i], map.get(nums1[i])+1);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (map.containsKey(nums2[i])) {
                res.add(nums2[i]);
                map.remove(nums2[i]);
            }
        }
        int[] ans = new int[res.size()];
        for (int i = 0; i < ans.length; i++) ans[i] = res.get(i);
        return ans;
    }
}

其它三种关于HashSet,Binary Search和Two Pointer的解法,见LC论坛

Intersection of Two Arrays II

Problem

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

Example

Given nums1 = [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 num2'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?

If only nums2 cannot fit in memory, put all elements of nums1 into a HashMap, read chunks of array that fit into the memory, and record the intersections.
  
If both nums1 and nums2 are so huge that neither fit into the memory, sort them individually (external sort), then read 2 elements from each array at a time in memory, record intersections.

Solution

1. 8ms

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> res = new ArrayList();
        Map<Integer, Integer> map = new HashMap();
        //Using HashMap to store values in nums1[]
        for (int i = 0; i < nums1.length; i++) {
            if (!map.containsKey(nums1[i])) map.put(nums1[i], 1);
            else map.put(nums1[i], map.get(nums1[i])+1);
        }
        //Modify the map with the amount of equal keys in nums2
        for (int i = 0; i < nums2.length; i++) {
            //Make sure the value of nums2[i] in map is larger than 0
            if (map.containsKey(nums2[i]) && map.get(nums2[i]) > 0) {
                res.add(nums2[i]);
                map.put(nums2[i], map.get(nums2[i])-1);
            }
        }
        //Transform ArrayList() to int[]
        int[] ans = new int[res.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = res.get(i);
        }
        return ans;
    }
}

2. 4ms

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        int k = 0, l1 = nums1.length, l2 = nums2.length;
        int[] result = new int[l1];
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        //After sorted, just so easy
        for (int i = 0, j = 0; i < l1 && j < l2;)
            if (nums1[i] < nums2[j]) i++;
            else if (nums1[i] == nums2[j++]) result[k++] = nums1[i++];
        return Arrays.copyOf(result, k);
    }
}