leetcode349—Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4]
Note:
- Each element in the result must be unique.
- The result can be in any order.
想法:先找出两个vector中相等的元素,放入另外一个vector中。然后去掉重复元素
注意:vector中的成员函数unique()只是去除相邻的重复元素,因而需要对vector内部的元素进行排序。但是当执行unique()函数后,去除的重复元素仍然存在,因而需要使用erase()完全去除尾部的元素。
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
; i < nums1.size() ; i++){
;j<nums2.size() ; j++){
if(nums1.at(i) == nums2.at(j)){
result.push_back(nums1[i]);
}
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(), result.end()), result.end());
return result;
}
};
leetcode349—Intersection of Two Arrays的更多相关文章
- [leetcode349]Intersection of Two Arrays
设计的很烂的一道题 List<Integer> res = new ArrayList<>(); // int l1 = nums1.length; // int l2 = n ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- Intersection of Two Arrays | & ||
Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...
随机推荐
- Open Credit System(UVA11078)
11078 - Open Credit System Time limit: 3.000 seconds Problem E Open Credit System Input: Standard In ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
- Bash:字符串操作
参考:http://blog.csdn.net/finewings/article/details/5718133 字符串提取 去掉指定前缀 1. ${varible#pattern} ...
- php两个多维数组组合遍历
$res = $this->LoanRecord->searchloan($conditions,$columns,$page,$this->num,$user_id); forea ...
- CentOS7上Python3.5安装
CentOS7上Python3.5安装 1.下载 https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.上传到服务器 3. yum in ...
- 记一次寻找appbug的问题
公司规模 3000人以上 全国500强. 从总部刚交接过来的代码. 1.找不到代码.代码部分丢失.(由于没有交接,没有任何相关文档,花了一天确定代码丢失.从总部找到部分代码) 2.查找测试库,发现测试 ...
- 2018-10-27 22:44:33 c language
2018-10-27 22:44:33 c language 标准的C语言并不支持上面的二进制写法,只是有些编译器自己进行了扩展,才支持二进制数字.并不是所有的编译器都支持二进制数字,只有一部分编译 ...
- Core Animation-1:图层树
图层的树状结构 >巨妖有图层,洋葱也有图层,你懂吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做*Lay ...
- Javascript基础笔记(部分)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python基础学习14----正则表达式
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 在python中正则表达式被封 ...