LeetCode-349 Intersection of Two Arrays
来源:互联网 发布:万方数据库检索式 编辑:程序博客网 时间:2024/06/08 04:56
https://leetcode.com/problems/intersection-of-two-arrays/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
1、先排序,再用两指针分别扫描两数组(12ms)
class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> num;
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
int i = 0,j = 0;
while(i<nums1.size() && j < nums2.size()){
if(nums1[i] == nums2[j] && find(num.begin(),num.end(),nums1[i]) == num.end()){
num.push_back(nums1[i]);
i++;
j++;
continue;
}
if(nums1[i]<nums2[j])
i++;
else
j++;
}
return num;
}
};
2、发现STL中有set_intersection()函数,是对于排好序后的两个集合(即不会有重复元素)求交集,因此这里调用该函数的话,会有重复元素出现。
2.1利用unique、erase去重:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
vector<int> num(100000); // 数组长度影响:100000——28ms,500——12ms
auto iter = set_intersection(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),num.begin());
num.resize(iter-num.begin());
vector<int>::iterator iter1 = unique(num.begin(),num.end());
num.erase(iter1,num.end());
return num;
}
};
2.2直接遍历去重:
class Solution {public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
vector<int> num(1000),result;// 同上,数组长度影响:100000——28ms,(500、1000等)——12ms
auto iter = set_intersection(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),num.begin());
num.resize(iter-num.begin());
int len = num.size();
for(int i = 0;i<len;i++){
if(! result.empty() && num[i]==result.back())
continue;
result.push_back(num[i]);
}
return result;
}
};
- Leetcode-349 Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays C++
- LeetCode 349 Intersection of Two Arrays
- LeetCode-349&350.Intersection of Two Arrays
- LeetCode-349 Intersection of Two Arrays
- leetcode-Intersection of Two Arrays-349
- LeetCode 349 Intersection of Two Arrays
- LeetCode#349 Intersection of Two Arrays
- LeetCode(349)Intersection of Two Arrays
- [LeetCode]349 Intersection of Two Arrays
- [LeetCode-349] Intersection of Two Arrays(java)
- Leetcode 349 Intersection of Two Arrays
- LeetCode No.349 Intersection of Two Arrays
- leetcode-349-Intersection of Two Arrays
- [Leetcode 349]Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays
- Leetcode #349 Intersection of Two Arrays
- LeetCode 349 Intersection of Two Arrays
- Hive的HQL语句及数据倾斜解决方案
- JavaScript DOM 获取焦点然后逐行显示
- java对字符的编码处理
- Texturepack、physicsEditor、spriteilluminator的安装包下载
- React知识库内容精选:10篇文章让你迅速了解该技术
- LeetCode-349 Intersection of Two Arrays
- eclipse如何修改dynamic web module version
- 常用的一些HQL查询语句详解(2)
- Android源码下载 清华AOSP镜像源
- 表数据的插入,更新,删除
- Swift-Xcode真机运行出现Reason: image not found错误
- 支付遇到的问题
- JavaScript 学习笔记--02
- thrift使用http并部署到iis