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这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- java web中jsp,action,service,dao,po分别是什么意思和什么作用
JSP:全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1] 是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动 ...
- WPF学习(一)--布局控件简介
WPF的4种基本布局介绍 1.Grid的布局 这个就没啥特别好说的,其实,基本上复杂的布局,都需要用到Grid. 主要就是对行和列进行进行设置和定义. 1.行表格 列表格: 包含行和列的表格 2.St ...
- zoj3890 BFS
就是搜. #include<stdio.h> #include<string.h> #include<queue> using namespace std; #de ...
- 用freemarker生产静态页面
FreeMarker概述 * FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 * Template + data model = output ...
- mysql中的if判断
问题是这样的,有一张表(tb_class)专门保存班级的ID和班级的名字 另一张表是学生信息表(tb_stu),表中有一个字段叫classID,没有外键关联,现在要把 这张表刷新到另一个表tb_par ...
- Codeforces 650B Image Preview
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- FatMouse的交易问题
想按照某个值排序,用sort()函数,结果想了半天不知道用数组怎么解决,然后看了答案,才知道原来可以用struct,想想我真是笨死了.. 原题描述以及答案如下: Problem Description ...
- eclipse中新建javaweb项目,查看某些类的源码
网上查到的大多说在项目下选择properties,在java Build Path下Add ExternalJAR Selection,加上Tomcat->lib下的servlet-api.ja ...
- xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi
xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") d ...
- 锋利的jQuery-7--一个$.fn.color插件的编写过程
编写一个设置和获取元素的color的插件: 首先实现第一个功能,设置: ;(function($){ $.fn.extend({ color:function(value){ return this. ...