LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/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.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
题解:
可以使用双指针.
Time Complexity: O(nlogn). Space: O(1).
AC Java:
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
List<Integer> res = new ArrayList<Integer>();
while(i<nums1.length && j<nums2.length){
if(nums1[i] < nums2[j]){
i++;
}else if(nums1[i] > nums2[j]){
j++;
}else{
res.add(nums1[i]);
i++;
j++;
}
}
int [] resArr = new int[res.size()];
int k = 0;
for(int num : res){
resArr[k++] = num;
}
return resArr;
}
}
Could use HashMap as well.
Time Complexity: O(m + n).
Space: O(Math.min(m, n)).
AC Java:
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if(nums1 == null || nums2 == null){
return new int[0];
}
HashMap<Integer, Integer> hm = new HashMap<>();
for(int num : nums1){
hm.put(num, hm.getOrDefault(num, 0) + 1);
}
List<Integer> res = new ArrayList<>();
for(int num : nums2){
if(hm.containsKey(num)){
res.add(num);
if(hm.get(num) == 1){
hm.remove(num);
}else{
hm.put(num, hm.get(num) - 1);
}
}
}
int [] resArr = new int[res.size()];
int i = 0;
for(int num : res){
resArr[i++] = num;
}
return resArr;
}
}
Follow up 2 nums1 length is smaller. 用双指针先sort两个array明显没有利用到num1.length小的特性. 若是用HashMap来记录num1每个element出现频率再iterate nums2, 那么Time Complexity: O(m + n), m = nums1.length, n = num2.length. Space: O(m).
或者sort nums1 再对每一个num2的element在 sorted nums1上做 binary search. Time Complexity: O(mlogm + nlogm). Space: O(1).
由此可见,当m很小时,用HashMap和binary search就是time和space的trade off.
Follow up 3 nums2 is sorted but too big for memory. I/O的操作很贵,所以首先想到的是避免I/O的次数。
若是nums1可以全部load到memory上, 先sort nums1再把nums2从小到大分开load到memory来.
if load进来这一段最大值, 也就是最后一个值<nums1[0] 或者 load进来这一段最小值, 也就是第一个值>nums1[nums1.length-1]可以直接跳过, load下一段. else load进来这一段 和 sorted nums1做双指针.
若是nums1也太大了,就先external sort nums1, 在分开load进来nums1一段和nums2一段做双指针.
类似Find Common Characters, Intersection of Two Arrays.
LeetCode Intersection of Two Arrays II的更多相关文章
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [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] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- LeetCode_350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...
随机推荐
- iOS 英文学习
deprecated 废弃的 NS_DEPRECATED_IOS(2_0, 7_0)2.0开始使用,7.0废弃 NS_AVAILABLE_IOS(7_0) 7.0之后有效 Indicator 指示器 ...
- PHP上传文件示例
虽然大多数人认为Web只包含网页,但HTTP协议实际上可以传输任何文件,如office文档.PDF.可执行文件.AVI.压缩文件及各种其他文件类型.虽然FTP在历史上一直是向服务器上传文件的标准方式, ...
- spring 的IoC的个人理解
1.ioc IoC的概念介绍 ( a).依赖注入, 分为依赖 和 注入 , 其实依赖是一种耦合方式, 通过spirng在运行时将这种依赖关系完成, 达到解耦目的, 同时达到代码重用, 方便测试,更加 ...
- hdu 1300 Pearls
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 思路:用dp[i]表示前i种花费最低的情况,则有dp[i]=min(dp[i],dp[j+1]+(( ...
- Java正则表达式实现字符串的动态多替换
需求场景: 今天在处理SQL语句的时候,由于数据库中存的格式是VARCHAR2型的,这就需要对SQL语句中WHERE条件后边的带数字的字符串加上单引号,对于字符串的处理,首先想到的就是正则表达式,对正 ...
- UVA 11481 Arrange the Numbers(组合数学 错位排序)
题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...
- 8-05分支结构CASE..END
语法: CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 ...ELSE 其他结果 END 执行顺序: 条件1成立执行结果1 条件2成立执行结果2 如果所有WH ...
- UWP webview 键盘bug,回退页面,键盘会弹一下。
最新项目发现一个关于Webview的键盘bug. 具体问题:当点击Webview 网页里面input之类的东东,输入键盘会弹出来,这个时候,按回退键,键盘会收起来,再按回退键,界面会退到前一个页面,但 ...
- 数据库密码爆破HexorBase
数据库密码爆破HexorBase 数据库服务是服务器上最常见的一类服务.由于数据库保存大量的敏感信息,所以它的安全非常重要.测试数据库服务安全的重要方式,就是检查口令的强壮度. Kali Li ...
- JavaScript第一天
1.静态的网页技术和动态的网页技术 静态网页是相对于动态网页而言,是指没有后台数据库.不含程序和不可交互的网页.你编的是什么它显示的就是什么.不会有任何改变.静态网页相对更新起来比较麻烦,适用于一般更 ...