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这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- .NET Core 在Visual Studio 2015 下的使用-MSDN
.NET Core RC2 现已推出,这是真正的"候选发布"而非 RC1 Beta 冒充的候选发布(如果是那样,请考虑发布后出现的所有更改).当前,围绕 .NET Core 的开发 ...
- form表单提交和ajax提交的区别
form表单是整个页面跳到服务器的地址然后提交数据: ajax是往这个地址post数据 <form style="padding:0px;margin:0px;" targe ...
- js获取select改变事件
js获取select改变事件onchage前的值 和 onclick事件 <select id="wupin_id" name="wupin_id" on ...
- 【CodeForces 520E】Pluses everywhere
题意 n个数里插入k个+号,所有式子的和是多少(取模1000000007) (0 ≤ k < n ≤ 105). 分析 1.求答案,考虑每个数作为i位数(可为答案贡献10的i-1次方,个位i=1 ...
- c语言的数学函数ceil、floor、round
头文件<math.h> 函数原型和作用 double ceil(double x); 向上取整 double floor(double x); 向下取整 double round(doub ...
- BZOJ-1934 Vote 善意的投票 最大流+建图
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1551 Solved: 951 [Submit][S ...
- 学习笔记 BIT(树状数组)
痛定思痛,打算切割数据结构,于是乎直接一发BIT 树状数组能做的题目,线段树都可以解决 反之则不能,不过树状数组优势在于编码简单和速度更快 首先了解下树状数组: 树状数组是一种操作和修改时间复杂度都是 ...
- Spring JdbcTemplate 的使用与学习
JDBCTemplate 是SPRING 框架自带的一种对sql 语句查询的封装 ,封装非常完善,虽然与Hibernate比起来有一点麻烦,但是学号JDBCTemplate可以让我们用Spirngmv ...
- tp 多语言支持
tp支持多语言 通过get来改变语言的 http://localhost/tp/index.php/Admin/User/add/hl/zh-cn http://localhost/tp/index. ...
- --hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
解题思路: 多重背包:第 i 件物品有 j 个可用. 本题中 第 p[i] 类大米 有 c[i] 袋大米可买 ,故本题为多重背包. n(总钱数).m(种类) p[i] 单价 h[i] 重量 c[i] ...