题解 | #两个数组的交集#
两个数组的交集
https://www.nowcoder.com/practice/56ea71d1f4e94de2aaec10e985874cce
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { //建立一个哈希数组 bool hash[1010] = {0}; //将答案存放到ret中 vector<int> ret; //将 nums1的元素标记为true for(auto e : nums1) { hash[e] = true; } //遍历nums2 for(auto e : nums2) { //若为真为公共数字 if(hash[e]) { //存到ret中 ret.push_back(e); //避免重复,所以在hash中删除这个数字 hash[e] = false; } } return ret; } };