2017-09-01 13:51:38 +0000   |     algorithm leetcode hash table two pointers   |   Viewed times   |    

题目

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 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?

未排序的情况,用HashMap,复杂度

HashMap统计nums1中所有元素的频率。然后再遍历nums2,到HashMap里查找元素。

代码

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer,Integer> map = new HashMap<>();
        for (Integer n : nums1) {
            Integer freq = map.get(n);
            map.put(n,(freq == null)? 1 : freq+1);
        }
        int[] inter = new int[Math.min(nums1.length,nums2.length)];
        int cur = 0;
        for (Integer n : nums2) {
            Integer freq = map.get(n);
            if (freq != null) {
                inter[cur++] = n;
                if (freq == 1) {
                    map.remove(n);
                } else {
                    map.put(n,freq-1);
                }
            }
        }
        return Arrays.copyOfRange(inter,0,cur);
    }
}

结果

intersection-of-two-arrays-two-1

有序数组,可以用Two Pointers算法

和合并排序一样的,用两个指针分别指向两个数组。代码省略。

如果nums1长度大大小于nums2的长度?

这就意味着有序数组的Two Pointers算法效率可能高于无序数组的HashMap的方法。假设nums1.length = n, nums2.length = m。无序数组的HashMap方法的复杂度是 ,有序数组的Two Pointers方法的复杂度是 。当m很大,而n很小的时候,\log_{}{m}就很重要了。

如果nums1nums2都很大,内存放不下,怎么办?

可以分块读取一部分的nums1nums2,最后可以把所有小块交集合并起来。