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;
    }
}


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