【leetcode】349. 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.
我们可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的开头,
然后比较两个数组的大小,把小的数字的指针向后移,如果两个指针指的数字相等,就加入到结果中。
但是结果中的数字不可以重复,可以用unique然后erase。(好像有些麻烦)也可以在结果中判断最后一个数字
是不是和当前的数字相等(因为已经sort过了)或者结果是不是empty,记住判断empty要在前面,否则直接
对一个空的vector求back会报错。
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> ret;
if(nums1.size()==||nums2.size()==)
return ret;
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
auto i=nums1.begin();
auto j=nums2.begin();
while(i!=nums1.end()&&j!=nums2.end()){
if(*i<*j)
i++;
else if(*i>*j)
j++;
else {
if(ret.empty()||ret.back()!=*i)
ret.push_back(*i);
i++;
j++;
}
}
return ret;
}
};
【leetcode】349. Intersection of Two Arrays的更多相关文章
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- 【一天一道LeetCode】#349. Intersection of Two Arrays
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【easy】349. Intersection of Two Arrays
找两个数组的交集(不要多想,考虑啥序列之类的,就是简单的两堆数求交集啊!!!最后去个重就好了) //LeetCode里用到很多现成函数的时候,苦手だな- //这个题的思路是,先sort,把两个vect ...
- 【LeetCode】350. Intersection of Two Arrays II 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Java排序+双指针 Python排序+双指针 Python解 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
随机推荐
- Java IO设计模式(装饰模式与适配器模式)
01. 装饰模式 1. 定义 Decorator装饰器,就是动态地给一个对象添加一些额外的职责,动态扩展,和下面继承(静态扩展)的比较.因此,装饰器模式具有如下的特征: 它必须持有一个被装饰的对象(作 ...
- C#语法糖演进-C#语言和.NET Framework平台介绍
p { font-size: 14px; text-indent: 2em } .title { text-indent: 0; font-size: 22px; font-weight: bold; ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- CentOS 6.5 下安装 Redis 2.8.7(转载)
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...
- 使用Vue-resource完成交互
使用vue-resource 引入vue-resource vue-resource就像jQuery里的$.ajax,是用来跟后端交互数据的,vue-resource是vue的一个插件,所以我们在开始 ...
- hdu_1695: GCD 【莫比乌斯反演】
题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...
- CSS学习笔记一:css 画平面图形
最近在学习CSS,先从CSS画点平面图形入手,发现除了正方形.长方形此类比较简单,只要有长宽设置恰当即可,画圆要涉及radius,然后恢复到做界面的最讨厌的状态了,不断的修改设值,调整数据,所幸并不多 ...
- mysql中csv文件的导入导出
导出例子: select *from testinto outfile 'D:test.csv'fields terminated by ',' optionally enclosed by '&qu ...
- Cocoapods最新安装教程
一.CocoaPods简介 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如 Java 语言的 Maven,nodejs 的 npm.随着 iOS 开发者的增多,业界也出现了为 iOS 程序提 ...
- C语言编译过程(转)
内容摘要 : C语言编译的整个过程是非常复杂的,里面涉及到的编译器知识.硬件知识.工具链知识都是非常多的,深入了解整个编译过程对工程师理解应用程序的编写是有很大帮助的,希望大家可以多了解一些,在遇到问 ...