[leetcode]349. Intersection of Two Arrays数组交集
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
题意:
如题
Solution1: HashSet
1. Scan nums1, put items in nums1 to set1
2. Scan nums2, check each item is contained in set1
注意: HashSet.contains() -> O(1)
code
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet <Integer> set1 = new HashSet<>();
HashSet <Integer> resSet = new HashSet<>(); for (int i = 0; i < nums1.length; i ++) {
set1.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i ++) {
if (set1.contains(nums2[i])) {
resSet.add(nums2[i]);
}
}
// resSet -> int[]res
int [] res = new int[resSet.size()];
int index = 0;
for (int num : resSet) {
res[index++] = num;
}
return res;
}
}
[leetcode]349. Intersection of Two Arrays数组交集的更多相关文章
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: 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 ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
- LeetCode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 15. leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- vcenter修改用户密码的方法
https://192.168.x.x:9443登录,必须用administrator@vsphere.local登录,不能用root用户登录. 主页-系统设置- Single Sing-On-用户和 ...
- 基于Vue的Ui框架
基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 http://element.eleme.io/ MintUi 基于vue 移动 ...
- django路由初识
静态文件配置 1.项目下面新建一个文件夹static settings.py中最后添加 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static ...
- python 命名元组(namedtuple)
我们知道c/c++语言中,有结构体这种数据类型: struct{ string name; int age; char sex; }student; 在对结构体对象进行赋值或者取值时可以使用.运算 ...
- java字符串分解 StringTokenizer用法
Java中substring方法可以分解字符串,返回的是原字符串的一个子字符串.如果要讲一个字符串分解为一个一个的单词或者标记,StringTokenizer可以帮你. 先看个例子: 1 public ...
- metaq架构原理
原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2034111 本来只是想看下metaq的文档,结果发现好乱,现在metaq其实有两个大分支了,一个是庄晓丹 ...
- Coxph model Pvalue Select
I am calculating cox propotional hazards models with the coxph function from the survival package. ...
- java web 读取文件,文件路劲不对的问题
都知道,一般java项目,编译后的文件是在classes文件夹下面: 而java web项目,则是在WEB-INF/classes文件夹下面.new File(fileName)须先获取tomcat中 ...
- 进程和创建线程的两种方法(threading.Thread)
进程 如QQ 要以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的管理, 网络接口的调用等,进程就是各种资源管理的集合 线程:是操作系统最小的调度单位,是一串指令的结合 进程 要操作 ...
- canal HA配置
https://github.com/alibaba/canal/wiki/AdminGuide#ha%E6%A8%A1%E5%BC%8F%E9%85%8D%E7%BD%AE HA模式配置 1. 机器 ...