leetcode修炼之路——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 static int[] intersect(int[] nums1, int[] nums2) {
List<Integer> reslut = new ArrayList<>();
Arrays.sort(nums1);// 先进行数组排序
Arrays.sort(nums2);
int position = 0;// 记录位置
for (int i = 0; i < nums2.length; i++) {
if (position == nums1.length) {// 终止条件:如果位置已经是最后一个了,终止
break;
}
for (int j = position; j < nums1.length; j++) {
if (position < nums1.length) {
if (nums2[i] == nums1[j]) {
position = j + 1;// 记录下当前相等的位置
reslut.add(nums2[i]);
break;
}
}
}
}
int[] resultNums = new int[reslut.size()];
for (int i = 0; i < reslut.size(); i++) {
resultNums[i] = reslut.get(i);
}
return resultNums;
}
上面算法很简单,就是先对数组进行排序,然后遍历,记录相等时另一个数组的位置,然后放入list中。
leetcode修炼之路——350. Intersection of Two Arrays II的更多相关文章
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 两个数组相交之二
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] ...
随机推荐
- 如何在js文件中实现获取request.getCotextPath();
我们在jsp中可以方便的使用“request.getCotext()”来获取工程的根目录. 但是如果我们的js代码存在一个单独的js文件中,这时候再想获取根目录,我们就要自己截取了.可以采用下面的方式 ...
- Unity Time的使用
脚本语言:C# 1.deltatime: deltatime它表示距上一次调用Update或FixedUpdate所用的时间,调用deltatime可以使物体的旋转以一种恒定的速度来运行,而不受帧速率 ...
- 【高精度】Vijos P1010 清帝之惑之乾隆
题目链接: https://vijos.org/p/1010 题目大意: 多组数据,求R的n次幂(R为不超过9999.9的小数 n<=200)R保证占6位 不输出前导0和后缀0,整数就只输出整数 ...
- Rectangle Area——LeetCode
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
- www.chenbowenblog.com 博客地址转移
我的新博客地址是 www.chenbowenblog.com 欢迎来访.
- android快捷简单的实现音乐播放器
自己做了一个相对完整的音乐播放器,现在把播放模块提取出来,分享给大家.音乐播放器基本功能都实现了的,可能有些BUG,希望谅解. 播放器功能如下: 1.暂停,播放 2.拖动条实现,快进,快退 3.歌词同 ...
- javascript变量 数组 对象
一 变量 1.全局变量和局部变量 在JavaScript中同一个变量可以反复赋值,而且可以是不同类型的变量,但是要注意只能用var声明一次.这种变量类型不固定的语言称为动态语言,与之对应的静态语言,如 ...
- 在SQL中使用PL/SQL函数存在的问题
-----------------------------Cryking原创------------------------------ -----------------------转载请注明出处, ...
- Sql Server 2005 CLR实例
本文转载:http://www.cnblogs.com/yongfa365/archive/2010/04/26/SQL-Server-CLR.html CSDN:博客参考http://blog.cs ...