题解 | 两个数组的交集

两个数组的交集

https://www.nowcoder.com/practice/f31371f27dcd4c90a8fd2902d3e4592c

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums1 int整型vector 
     * @param nums2 int整型vector 
     * @return int整型vector
     */
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,bool>mp;
        vector<int>rs;
        for (int i:nums1)
        {
            if (mp.find(i) == mp.end())
                mp[i] = true;
        }
        for (int i:nums2)
        {
            if (mp[i])
            {
                mp[i] = false;
                rs.push_back(i);
            }
        }
        sort(rs.begin(),rs.end());
        return rs;
    }
};

RogeAustine题解系列 文章被收录于专栏

这里是RogeAustine的题解专栏,里面包含的题目都是十分典型的经典题目。

全部评论

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务