[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这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- [UE4]Acotr
任何能被放在关卡中的对象都是Actor Tick是每帧都会调用的事件
- [UE4GamePlay架构(九)GameInstance(转)
GameInstance这个类可以跨关卡存在,它不会因为切换关卡或者切换游戏模式而被销毁.然而,GameMode和PlayController就会再切换关卡或者游戏模式时被引擎销毁重置,这样他们里面的 ...
- 6.15-初识JSP、javaweb
一.javaweb web服务器 tomcat C/S 客户端/服务器 B/S 浏览器/服务器 URL: http协议 https 加密的协议 localhost 127.0.0.1 常用web服务器 ...
- var与dynamic
var与dynamic 如果你用MVC写过程序,那么你应该知道ViewBag这个用于前后台的数据传递工具,那么你是否对ViewBag的用法感到过疑惑呢? ViewBag.Mode1l=new obje ...
- python中的运算符及表达式及常用内置函数
知识内容: 1.运算符与表达式 2.for\while初步了解 3.常用内置函数 一.运算符与表达式 python与其他语言一样支持大多数算数运算符.关系运算符.逻辑运算符以及位运算符,并且有和大多数 ...
- PHP写日志公共类
Txl_Log.php <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * * ...
- Linux开机挂载windows共享文件夹
https://blog.csdn.net/zhaogang1993/article/details/79573271 (可行) 命令: mount -t cifs -o username=&quo ...
- Linux 配置TomCat 项目三大步骤
一: 安装 JRE 01: 下载 server-jre 安装包 => http://www.oracle.com/technetwork/java/javase/downloads/serve ...
- 1.HTML编码解码URL替换--代码整理
public class HtmlCode { public static String encode(String str){ String s = ""; if (str.le ...
- 微信公众号自动回复 node
纯属分享记录: app.js var bodyParser = require('body-parser'); require('body-parser-xml')(bodyParser); var ...