LeetCode笔记:349. Intersection of Two Arrays
来源:互联网 发布:网络用语麻辣烫 编辑:程序博客网 时间:2024/06/06 14:03
问题:
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.
大意:
给出两个数组,写一个函数来计算他们的交叉点。
例子:
给出数组 nums1 = [1,2,2,1],nums2 = [2,2],返回[2]。
注意:
在结果中每个元素必须是唯一的。
结果可以是乱序的。
思路:
这个问题思路倒是有的,不过一开始我的返回值没有做处理,导致一直报错,折腾一番后发现还是最初的想法比较好。
先说最初的想法错误的以为不行后尝试的简单方法,就是遍历第一个数组,对其中每个数字在第二个数组中找是否有,如果找到了,就放入结果数组中,当然结果数组因为要求每个数字都是唯一的,所以也要再检查一遍这个数字在结果数组中是否出现过,这个方法循环套循环,想来也是比较耗时的,虽然可以在找到交叉点数字后在第二个数组中去掉该数字做一点优化,但依然比较耗时。
现在回到最初的想法,先给两个数组分别排序后,同时从两个数组的第一个数字开始比较,同时各自设置一个标记,记录当前数组中比较到哪个位置了,如果哪个数组中的数字小一些,就将其标记往后移,再比较大一些的那个数字。如果发现比较的两个数字相等,则说明交叉了,就要考虑放到结果数组中了,放的时候要检查一下之前有没有放入过,但是因为放到结果数组中的数字一定也是有序的,所以只用比较和结果数组中上一个数字是不是相同就可以了,这样同样节省了时间,让后两个数组中的标记都往后移一位继续比较。这里移位的时候要注意一点,for循环如果是以一个数组的长度来当做结束判断条件的,那么在对另一个数组的标记做移位时每次都要判断是不是已经到最后一位了,否则会超出数组的,这里很容易忽略。
因为我们一开始创建结果数组时肯定是以其中一个数组的长度去创建的,但是最终返回时必须要处理一下,只能返回有数字的那部分长度,否则会报错。这些都是坑。
这个做法除了一开始的排序外,剩下的比较的复杂度因为边遍历边比较,只遍历了一次,还是同时遍历的,而且判断结果数组中是否重复时只用和上一位数字比较,所以只有O(n),还是比较快的,我做出来的时间也是3ms,挺快的。
代码(Java):
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { int results[] = new int[nums1.length];// 结果数组 // 没有数字的情况 if (nums1.length == 0 || nums2.length == 0) { return new int[0]; } // 先对两个数组排序 Arrays.sort(nums1); Arrays.sort(nums2); int index = 0; for (int i = 0,j = 0; i < nums1.length;) { if (nums1[i] < nums2[j]) {// 第一个数组中数字小 i++; } else if (nums1[i] == nums2[j]) {// 数字相同,判断是否曾经放入过结果数组,然后放入 if (index == 0) { results[index] = nums1[i]; index++; } else if (results[index-1] != nums1[i]) { results[index] = nums1[i]; index++; } i++; // 判断第二个数组有没有到最后一个数字,还没到才继续往后移去比 if (j < nums2.length-1) j++; else break; } else {// 第二个数组中数字小,注意要判断是否到达最后一个数字 if (j < nums2.length-1) j++; else { i++; break; } } } return Arrays.copyOfRange(results, 0, index);// 结果数组只返回有数字的那一部分 }}
- LeetCode笔记:349. Intersection of Two Arrays
- LeetCode笔记:349. Intersection of Two Arrays
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode题解 349. Intersection of Two Arrays
- LeetCode - 349. Intersection of Two Arrays
- [Leetcode]349. Intersection of Two Arrays
- android关于序列化和反序列化List<? implement Parcelable> list属性
- 基于webpack的前端工程化开发解决方案探所
- CCF 201512-3 画图
- 通过值查找数据库中特定表和列
- Node.js 历史
- LeetCode笔记:349. Intersection of Two Arrays
- 线程和进程区别
- 结构化数据、半结构化数据和非结构化数据
- MVC模式有哪些优缺点?
- 按界面空白处隐藏软键盘
- js中cookie的使用详细分析
- WIN7解决 Wamp 开启SSL失败 modules/mod_ssl.so
- 浅谈I2C的使用吧
- CCF 201512-2 消除类游戏