[LeetCode] Intersection of Two Arrays 两个数组

Posted Grandyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] Intersection of Two Arrays 两个数组相关的知识,希望对你有一定的参考价值。

 

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

Example 1:

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

Example 2:

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

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

 

这道题让找两个数组交集的部分(不包含重复数字),难度不算大,我们可以用个 HashSet 把 nums1 都放进去,然后遍历 nums2 的元素,如果在 HashSet 中存在,说明是交集的部分,加入结果的 HashSet 中,最后再把结果转为 vector 的形式即可:

 

解法一:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> st(nums1.begin(), nums1.end()), res;
        for (auto a : nums2) {
            if (st.count(a)) res.insert(a);
        }
        return vector<int>(res.begin(), res.end());
    }
};

 

我们还可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的开头,然后比较两个数组的大小,把小的数字的指针向后移,如果两个指针指的数字相等,那么看结果 res 是否为空,如果为空或者是最后一个数字和当前数字不等的话,将该数字加入结果 res 中,参见代码如下:

 

解法二:

class Solution {
public:
    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 {
                if (res.empty() || res.back() != nums1[i]) {
                    res.push_back(nums1[i]);
                }
                ++i; ++j;
            }
        }
        return res;
    }
};

 

我们还可以使用二分查找法来做,思路是将一个数组排序,然后遍历另一个数组,把遍历到的每个数字在排序号的数组中用二分查找法搜索,如果能找到则放入结果 set 中,这里我们用到了set的去重复的特性,最后我们将set转为vector即可:

 

解法三:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> res;
        sort(nums2.begin(), nums2.end());
        for (auto a : nums1) {
            if (binarySearch(nums2, a)) {
                res.insert(a);
            }
        }
        return vector<int>(res.begin(), res.end());
    }
    bool binarySearch(vector<int> &nums, int target) {
        int left = 0, right = nums.size();
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return true;
            else if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        return false;
    }
};

 

或者我们也可以使用STL的 set_intersection 函数来找出共同元素,很方便:

 

解法四:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res;
        set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
        return vector<int>(res.begin(), res.end());
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/349

 

类似题目:

Intersection of Two Arrays II

Find Common Characters

 

参考资料:

https://leetcode.com/problems/intersection-of-two-arrays/

https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions

https://leetcode.com/problems/intersection-of-two-arrays/discuss/82001/8ms-concise-C%2B%2B-using-unordered_set

 

LeetCode All in One 题目讲解汇总(持续更新中...) 

以上是关于[LeetCode] Intersection of Two Arrays 两个数组的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 160. Intersection of Two Linked Lists

349. Intersection of Two Arrays

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

LeetCode:Intersection of Two Arrays

LeetCode Intersection of Two Arrays

LeetCode:Intersection of Two Linked Lists