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这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- Struts-1和2的比较
Struts1和Struts2都是MVC设计模式的经典应用框架,下文从代码,性能,测试,功能等方面对Struts1和Struts2进行简单比较,来看看Struts的高级版本在哪些方面进行了优化. (1 ...
- POJ-2352 Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
- 【bzoj1562】 NOI2009—变换序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...
- 【bzoj2115】 Xor
www.lydsy.com/JudgeOnline/problem.php?id=2115 (题目链接) 题意 给出一张图,可能有重边和自环,在图中找出一条从1-n的路径,使得经过的路径的权值的异或和 ...
- POJ2411 Mondriaan's Dream
Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...
- 轻量级应用开发之(06)Autolayout自动布局1
一 什么是Autolayout Autolayout是一种“自动布局”技术,专门用来布局UI界面的. 自IOS7 (Xcode 5)开始,Autolayout的开发效率得到很大的提高. 苹果官方也推荐 ...
- 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- char*和char []
1.char *s1 = "ssss"; 2.char s2[] = "bbbb"; 对于第一种,我是无法理解,无法想象字符串赋值给一个char类型的指针,查了 ...
- 随时修改添加,thinkphp小知识
__SELF__代表自身路径, 谁访问它代表是谁, 区别在于有没有传值.传值的话self会带那个值 __ACTION__ 找操作方法所在的位置,区别在于有没有传值.传值的话action不会带那个值 _ ...
- 如果你也和我一样,OSX反应慢,不妨试试这个