350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
代码如下:
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> list=new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
for(int i=0,j=0;i<nums1.length&&j<nums2.length;)
{
if(nums1[i]==nums2[j])
{
list.add(nums1[i]);
i++;
j++;
}
else if(nums1[i]<nums2[j])
i++;
else if(nums1[i]>nums2[j])
j++;
}
int[] result=new int[list.size()];
for(int i=0;i<list.size();i++)
result[i]=list.get(i);
return result;
}
}
350. Intersection of Two Arrays II的更多相关文章
- 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] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [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 ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- (Collection)350. Intersection of Two Arrays II
/* Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2 ...
随机推荐
- SQL 插入日期时间 变量值
--看看吧^^ CREATE TABLE #temp ( test datetime ) go --SQL: INSERT #temp SELECT 1.1 UNION ALL GO --SQL: I ...
- 本节向大家介绍一下UML建模误区
本节向大家介绍一下UML建模误区,这里向大家介绍九个误区,希望通过本节的学习,你对UML建模有清晰的认识,以免在以后使用过程中产生不必要的麻烦.下面让我们一起来看一下这些建模误区吧. UML建模误区 ...
- putty基本操作
1,进入全屏 标题栏右键,菜单中就有full screen选项. 2,退出全屏 鼠标移到左上角,单击鼠标左键,就会跳出菜单,full screen勾去掉. 3,从putty中复制内容到剪切板 鼠标左键 ...
- Mahout0.9的安装与测试
最近想实协同过滤的MR算法,但是网上查了一下,发现hadoop的生态系统中的Mahout的项目已经实现了相应的算法,因此想先尝试着实时这个mahout的使用及效果.要想用mahout必须要部署到had ...
- windows-ubuntu环境变量的设置格式的不同
1 在Ubuntu下输出环境变量,比如JAVA_HOME, 使用cat或者echo $JAVA_HOME即可,但是在windows下不可以, windows不支持cat命令,只能使用echo %JA ...
- GSM BTS Hacking: 利用BladeRF和开源BTS 5搭建基站
引文 如果你已经购买了Nuand(官方)BladeRF x40,那么就可以在上面运行OpenBTS并可以输入一些指令来完成一些任务.一般来说HackRF,是一款覆盖频率最宽的SDR板卡.它几乎所有的信 ...
- php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地
php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地 yeah,搞定啦 php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页 ...
- php大力力 [026节] php开发状态要随时做好整理工作
php大力力 [026节] php开发状态要随时做好整理工作: 1.整理了开发目录,以及文件命名: 2.做了各个页面的快捷方式: 3.把浏览器safari的很多没来得及消化的页面链接,写入了我的在线 ...
- poll函数和串口设置
2015.1.24 今天星期六,多云,早晨8:17起床的,今天是来南京起床最迟的一天,因为昨晚睡得有点迟,今天又不用上课,整个人有点放松.收拾好来到教室,教室门没有开,胡明也到了,其他人还在宿舍睡觉, ...
- .a包生成64位
./configure CC="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain ...