前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0349 - Intersection of Two Arrays

LeetCode 0349 - Intersection of Two Arrays

作者头像
Reck Zhang
发布2021-08-11 11:32:58
2030
发布2021-08-11 11:32:58
举报
文章被收录于专栏:Reck ZhangReck Zhang

Intersection of Two Arrays

Desicription

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

Example 1:

代码语言:javascript
复制
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

代码语言:javascript
复制
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.

Solution

代码语言:javascript
复制
class Solution {
public:
    std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
        auto set1 = std::set<int>();
        auto set2 = std::set<int>();
        for(auto num : nums1) {
            set1.insert(num);
        }

        auto res = std::vector<int>();
        for(auto num : nums2) {
            if(set1.find(num) != set1.end()) {
                set2.insert(num);
            }
        }

        for(auto num : set2) {
            res.push_back(num);
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-06-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Intersection of Two Arrays
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档