leetcode 350. Intersection of Two Arrays II

wenyq7發表於2020-10-16

題目:

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

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把兩邊同時出現的數字取出現次數最小的次數加入result就可以了。時間複雜度O(max(m ,n))吧。看了解答其實不需要兩個map,直接第一個map存完後在遍歷nums2的時候查它在不在map裡就行了,程式碼懶得寫了orz

Runtime: 3 ms, faster than 43.83% of Java online submissions for Intersection of Two Arrays II.

Memory Usage: 39.6 MB, less than 15.22% of Java online submissions for Intersection of Two Arrays II.

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> result = new ArrayList<>();
        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        for (int i : nums1) {
            map1.put(i, map1.getOrDefault(i, 0) + 1);
        }
        for (int i : nums2) {
            map2.put(i, map2.getOrDefault(i, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
            int key = entry.getKey();
            if (map2.containsKey(key)) {
                int value = Math.min(entry.getValue(), map2.get(key));
                for (int i = 0; i < value; i++) {
                    result.add(key);
                }
            }
        }
        int[] resultArray = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            resultArray[i] = result.get(i);
        }
        return resultArray;
    }
}

 

也可以先sort完了以後用two pointers O(n)一次解決。剛開始不知道為啥可能受了剛剛做duplicate的影響所以以為要整duplicate就寫的很麻煩(被註釋掉的部分),後來看了解答才發現想多了orz。

Runtime: 2 ms, faster than 96.63% of Java online submissions for Intersection of Two Arrays II.

Memory Usage: 39.2 MB, less than 15.22% of Java online submissions for Intersection of Two Arrays II.

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int p1 = 0;
        int p2 = 0;
        List<Integer> result = new ArrayList<>();
        while (p1 != nums1.length && p2 != nums2.length) {
            if (nums1[p1] < nums2[p2]) {
                p1++;
            } else if (nums1[p1] > nums2[p2]) {
                p2++;
            } else {
                /*int count1 = 1;
                while (p1 + 1 != nums1.length && nums1[p1] == nums1[p1 + 1]) {
                    p1++;
                    count1++;
                }
                int count2 = 1;
                while (p2 + 1 != nums2.length && nums2[p2] == nums2[p2 + 1]) {
                    p2++;
                    count2++;
                }
                int count = Math.min(count1, count2);
                for (int i = 0; i < count; i++) {
                    result.add(nums1[p1]);
                }*/
                result.add(nums1[p1]);
                p1++;
                p2++;
            }
        }
        int[] resultArray = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            resultArray[i] = result.get(i);
        }
        return resultArray;
    }
}

 

相關文章