349. Intersection of Two Arrays(C++实现)
来源:互联网 发布:java interface 缩写 编辑:程序博客网 时间:2024/05/16 15:53
答案一:
class Solution {public: vector<int> intersection(vector<int>& a, vector<int>& b) { sort(a.begin(),a.end()); sort(b.begin(),b.end()); vector<int> ans; for(int i=0,ll=0;i<b.size();i++) if(i==0||b[i]!=b[i-1]) { while(ll<a.size()&&a[ll]<b[i]) ll++; if(ll<a.size()&&a[ll]==b[i]) ans.push_back(b[i]); } return ans; }};
答案二:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {sort(nums1.begin(),nums1.end());sort(nums2.begin(),nums2.end());vector<int>res;const int n1=nums1.size();const int n2=nums2.size();int i=0,j=0;for(;i<n1&&j<n2;++i,++j){if(nums1[i]==nums2[j]){res.insert(res.begin(),nums1[i]);}if(nums1[i]<nums2[j])j--;else if(nums1[i]>nums2[j])i--;}for(i=1;i<res.size();++i){if(res[i]==res[i-1]){res.erase(res.begin()+i-1,res.begin()+i);i--;}}return res;}
0 0
- 349. Intersection of Two Arrays(C++实现)
- 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
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- linux进程管理
- XenServer网络堆栈Bridge模式介绍
- Http Test测试工具说明
- java反射机制怎样调用类的私有方法?
- 电话正则表达式
- 349. Intersection of Two Arrays(C++实现)
- 带分数---搜索
- Swift - Realm数据库的使用详解(附样例)
- android中使用iconfont图标字体
- 常用数据结构
- 细说 #pragma pack(n)
- 正则表达式
- 优化sql查询
- 利用java反射机制调用类的public公有方法的例子