LeetCode - 350. Intersection of Two Arrays II

题目:

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?

思路与步骤:

思路1:

很容易想到的一种,就是先对两个数组进行排序,然后挨个比较。


思路2:

和上一个不出现重复元素的 Intersection 一样,用 contains() 方法,由于重复元素需要出现,所以不用 HashSet ,用 List。

这里需要注意的是remove() 方法的使用

list1.remove((Integer)nums2[i]);
如果不强制转化成 Integer,后面的 nums2[i] 会被当做下标来处理。

思路3:

学习的别人的方法,用 HashMap。具体分析如下

首先将 HashMap 的 Key 设为 nums1[i],Value 设为 nums1[i] 出现过的次数。

然后在 nums2 数组中,若 HashMap 有 nums2[i] ,则 将 nums2[i] 存入一个 List,并且原来的 Value 值减1,直到减为 0 为止。


Follow up 问题解答

1. 以排序可按思路1 处理;

2. 如果 nums1 数组中元素少,则首先上述处理中,首先将nums2 放入  List 或者 HashSet 中,判断相等时再处理 nums1。

3. (引用LeetCode讨论中别人的解答)

     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:

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i = 0, j = 0;
        List<Integer> list = new ArrayList<Integer>();
        
        while(i<nums1.length && j<nums2.length){
            if(nums1[i] == nums2[j]){
                list.add(nums1[i]);
                i++;
                j++;
            } else if(nums1[i] < nums2[j])  i++;
            else if(nums1[i] > nums2[j])  j++;
        }
        int[] result = new int[list.size()];
        int c = 0;
        for (Integer num : list)   result[c++] = num;
        
        return result;
    }
}

Solution-2:

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list1 = new ArrayList<Integer>();
        for(int num : nums1)    list1.add(num);
        
        List<Integer> intersectionList = new ArrayList<Integer>();
        for(int i=0; i<nums2.length; i++)
            if(list1.contains(nums2[i])){
                intersectionList.add(nums2[i]);
                list1.remove((Integer)nums2[i]);
            }
            
        int[] result = new int[intersectionList.size()];
        for(int i=0; i<intersectionList.size(); i++)    result[i] = intersectionList.get(i);
        
        return result;
    }
}

Solution-3:

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        List<Integer> list = new ArrayList<Integer>();
        for(int num: nums1)  map.put(num, map.getOrDefault(num, 0)+1);
            //if(map.containsKey(num))    map.put(num, map.get(num)+1);
            //else    map.put(num, 1);
        for(int num2: nums2){
            if(map.containsKey(num2) && map.get(num2)>0){
                list.add(num2);
                map.put(num2, map.get(num2)-1);
            }
        }
        int[] result = new int[list.size()];
        for(int i=0; i<list.size(); i++)    result[i] = list.get(i);
        return result;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章