Binary Search:349. Intersection of Two Arrays
来源:互联网 发布:金牌网吧奖励软件 编辑:程序博客网 时间:2024/06/05 14:20
求两个数组相交的数字的集合,结果中不能有重复的。
想到了用set
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(), n = nums2.size(); set<int> res; vector<int> result; for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { if(nums1[i] == nums2[j]) { res.insert(nums1[i]); break; } } } for(auto a : res) { result.push_back(a); } return result; }};
看了大神代码,更简洁:
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> s(nums1.begin(), nums1.end()), res; for (auto a : nums2) { if (s.count(a)) res.insert(a); } return vector<int>(res.begin(), res.end()); }};
阅读全文
0 0
- Binary Search:349. Intersection of Two Arrays
- 349.Intersection of Two AND 350. Intersection of Two Arrays II Arrays leetcode binary search
- LeetCode-Easy部分中标签为 Binary Search 349. Intersection of Two Arrays 350. Intersection of Two Arrays II
- LeetCode-Easy部分中标签为 Binary Search 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays[][
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 被围绕的区域-LintCode
- Test 2017.10.20
- Oracle RAC相关问题——adding cluster entries toinittab
- Frida官方手册
- opencv中滑动条的创建与使用
- Binary Search:349. Intersection of Two Arrays
- [Android] 经验之谈,别再用Android开发宝AndBase了,以前的一个APP用了之后问题多多
- 牛客网错题总结集(一)
- R语言学习笔记(二)导入数据
- Python入门(简介、安装)
- NOIP2014 飞扬的小鸟
- VirtualBox安装使用
- 芯海钩沉 | 英特尔4004:我最贵,但也最慢
- 泛型