Intersection of Two Arrays | & ||
Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
分析:
这种考察一个数是否存在另一个集合中,一般来讲,都会用到HashSet. 如果不允许用额外的存储空间,可以对nums1排序,然后使用二分查找。
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
int[] arr = new int[];
return arr;
}
Set<Integer> set = new HashSet<>();
for (int i : nums1) {
set.add(i);
}
List<Integer> list = new ArrayList<>();
for (int i : nums2) {
if (set.contains(i)) {
list.add(i);
set.remove(i);
}
}
return list.stream().mapToInt(i->i).toArray();
}
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].
这一次需要把所有的都找出来,可以用HashMap<Integer, Integer> 来存储,第一个Integer指的是数,第二个指的是那个数存在的个数。
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
int[] arr = new int[];
return arr;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i : nums1) {
map.put(i, map.getOrDefault(i, ) + );
}
ArrayList<Integer> list = new ArrayList<>();
for (int i : nums2) {
if (map.containsKey(i) && map.get(i) >= ) {
list.add(i);
map.put(i, map.get(i) - );
}
}
return list.stream().mapToInt(i->i).toArray();
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Intersection of Two Arrays | & ||的更多相关文章
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given 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] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- 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 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- 总结css之内联图片的优缺点
会不会有这样一种感觉?IT技术开发知识面很广也很深,总会有你不懂得问题出现.一个接着一个新的问题,一个接着一个新的挑战. 今天,解读[内联图片],什么是内联图片,使用内联图片的优缺点是什么?这个问题是 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- POJ1737 Connected Graph
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- MySQL的表分区详解
这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...
- list的使用命令 百度经验保存
在key对应的list的头部添加字符串元素 命令:lpush #参数0 到-1 是从开始到结束 2 在key对应list的尾部添加字符串元素: 命令:rpush 3 在k ...
- Java & C++ 大数计算
Java--大数计算,妈妈再也不用担心我的学习了 . BigInteger 英文API: http://docs.oracle.com/javase/8/docs/api/ 中文API: http:/ ...
- 笔记:PHP查询mysql数据后中文字符乱码
新建表Clubs CREATE TABLE `Clubs` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET utf8 NOT NULL ...
- ResultSet
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码Statement stmt = con. ...
- Linux下memcache的安装和启动(转)
memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...
- 基于TcpListener的web服务器
写在前面 上篇文章根据<asp.net 本质论>书上提供的例子,实现了一个简单的web服务器,本篇文章将介绍另一种实现方式——基于TcpListener的web服务器. TcpListen ...