[leetcode350]Intersection of Two Arrays II求数组交集
List<Integer> res = new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i1 = 0;
int i2 = 0;
while (i1<nums1.length&&i2<nums2.length)
{
if (nums1[i1]<nums2[i2])
i1++;
else if (nums1[i1]>nums2[i2])
i2++;
else
{
res.add(nums1[i1]);
i1++;
i2++;
}
}
int[] num = new int[res.size()];
for (int i = 0; i < num.length; i++) {
num[i] = res.get(i);
}
return num;
受上一题的影响,本来想用hashset解决,但是发现不行,就换了排序然后遍历的方法,如果不相等,小的数的下边++,相等就添加
[leetcode350]Intersection of Two Arrays II求数组交集的更多相关文章
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: 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] ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- LeetCode_350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...
随机推荐
- charles 常用功能(八)重定向
1.点击鼠标右键 点击保存就保存到桌面上了 效果图 在123.txt中修改 然后另存为 点击红圈处 然后再次发送请求
- .Net Core JWT 动态设置接口与权限
通过上一篇.Net Core官方的 JWT 授权验证学习到了JWT的授权.可以发现一个问题,就是如果每个接口可以使用的角色都是写死的,这样如果有所修改会非常麻烦,虽然用policy可以一定程度上缓解, ...
- JQuery获取父,子,兄弟节点
jQuery.parent(expr) // 查找父节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent ...
- IAR环境定义位变量标志位 STM8 MSP430通用
首先建立一个公共点H文件,加入通用代码如下 typedef union { struct { unsigned char b0:1; unsigned char b1:1; unsigned char ...
- 软工个人项目 ——wc.exe
1.GitHub项目地址 https://github.com/k8kiw/WordCount 2.PSP预计时间 PSP2.1 Personal Software Process Stages 预估 ...
- 微信小程序template和组件
template主要是展示,主要是在调用的页面中定义.用import方式引入,然后去使用,通常是单独建立一个文件夹 去管理,文件夹有两个文件wxml和wxss,wxml中 可以定义多个template ...
- Spring framework核心
这一部分涵盖了Spring框架绝对不可或缺的所有技术. 1.IOC容器 1.1Spring IoC容器和beans介绍 org.springframework.beans和org.springfram ...
- 落谷P3041 [USACO12JAN]Video Game G
题目链接 多模式匹配问题,先建 AC 自动机. 套路性的搞个 DP: \(f[i][j]\) 表示前 \(i\) 个字符,当前在 \(AC\) 自动机上的节点是 \(j\) 能匹配到的最多分. 初始化 ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...
- mybatis逆向工程运行
命令: mvn mybatis-generator:generate 项目结构: generatorConfig.xml内容示例 <?xml version="1.0" en ...