[LeetCode]349. Intersection of Two Arrays 解题报告(C++)

[LeetCode]349. Intersection of Two Arrays 解题报告(C++)

题目描述

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 in the result must be unique.
  • The result can be in any order.

题目大意

  • 给了两个数组.
  • 让我们找到相同的元素.

解题思路

方法1:

  • 将两个数组各自放到 set 中.
  • 再两个 set 中找相同元素放进结果中.

代码实现:


class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        unordered_set<int> s(nums1.begin(), nums1.end());
        unordered_set<int> s2(nums2.begin(), nums2.end());

        for (auto x : s2) {
            if (s.count(x)) {
                res.push_back(x);
            }
        }
        return res;
    }
};

方法2:

  • 只是用一个 set .
  • 在找到元素之后.要将 元素从数组中删除.防止重复.

代码实现:

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

[LeetCode]350. Intersection of Two Arrays II 解题报告(C++)

题目描述

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:

  • 找重复的集合.
  • 1.找到相同元素.2.出现次数.
  • 用 map 构建各自数组字符和出现次数的关系.
  • 最后遍历某个数组(将这个数组重复元素去掉了)
  • 确定这个字符在两个数组都有.取最小的出现次数.加入输出结果中.

代码实现:

class Solution {
public:
    vector intersect(vector& nums1, vector& nums2) {

        map cnt1;
        map cnt2;
        set s(nums2.begin(), nums2.end());
        vector res;

        for (auto x : nums1) {
            cnt1[x]++;
        }
        for (auto x : nums2) {
            cnt2[x]++;
        }

        for (auto x : s) {

            if (cnt1.count(x) != 0 && cnt2.count(x) != 0) {
                int times = min(cnt1[x], cnt2[x]);
                for (int i = 0; i < times; i++) {
                    res.push_back(x);
                }
            }
        }
        return res;
    }
};

方法2:

  • 用哈希表来建立nums1中字符和其出现个数之间的映射
  • 然后遍历nums2数组,如果当前字符在哈希表中的个数大于0,则将此字符加入结果res中,然后哈希表的对应值自减1

代码实现:

class Solution {
public:
    vector intersect(vector& nums1, vector& nums2) {
        unordered_map m;
        vector res;
        for (auto a : nums1) ++m[a];
        for (auto a : nums2) {
            if (m[a]-- > 0) res.push_back(a);
        }
        return res;
    }
};

小结

    -

你可能感兴趣的:([解题报告])