[LintCode] Intersection of Two Arrays II 两个数组相交之二

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

Each element in the result should appear as many times as it shows in both arrays.
    The result can be in any order.

Example

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

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?

LeetCode上的原题,请参见我之前的博客Intersection of Two Arrays II

解法一:

class Solution {
public:
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        unordered_map<int, int> m;
        for (auto a : nums1) ++m[a];
        for (auto a : nums2) {
            if (m[a] > 0) {
                res.push_back(a);
                --m[a];
            }
        }
        return res;
    }
};

解法二:

class Solution {
public:
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        int i = 0, j = 0;
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        while (i < nums1.size() && j < nums2.size()) {
            if (nums1[i] < nums2[j]) ++i;
            else if (nums1[i] > nums2[j]) ++j;
            else {
                res.push_back(nums1[i]);
                ++i; ++j;
            }
        }
        return res;
    }
};
时间: 2024-12-18 08:09:24

[LintCode] Intersection of Two Arrays II 两个数组相交之二的相关文章

[LeetCode] 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 ord

350 Intersection of Two Arrays II 两个数组的交集 II

给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意:       输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致.       我们可以不考虑输出结果的顺序.跟进:    如果给定的数组已经排好序呢?你将如何优化你的算法?    如果 nums1 的大小比 nums2 小很多,哪种方法更优?    如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

[LeetCode][Python]Intersection of Two Arrays II

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

【leetcode】350. Intersection of Two Arrays II

problem 350. Intersection of Two Arrays II 参考 1. Leetcode_350. Intersection of Two Arrays II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10430296.html

[LintCode] Intersection of Two Arrays 两个数组相交

Given two arrays, write a function to compute their intersection.Notice Each element in the result must be unique.    The result can be in any order. Have you met this question in a real interview?Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], r

[LeetCode] NO. 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]. [题目解析] 这是对于求两个数组交集的延伸,之前求数组交集重复元素不包含在交集内,用set来求.那么这个问题,就可以用hashmap来解决.如下. public int[] intersect(int[] nums1, int

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

【leetcode76】Intersection of Two Arrays II

题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素累计计数 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: 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 i