[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]
Note:
- Each element in the result must be unique.
- The result can be in any order.
解法:由于结果中要求元素是唯一的,所以用set来统计num1 中的数字。再循环num2中的数字,在set中存在就记录到结果中,同时从set中删除。
Java: HashSet, T: O(n)
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet<Integer>();
ArrayList<Integer> res = new ArrayList<Integer>();
//Add all elements to set from array 1
for(int i =0; i< nums1.length; i++) set.add(nums1[i]);
for(int j = 0; j < nums2.length; j++) {
// If present in array 2 then add to res and remove from set
if(set.contains(nums2[j])) {
res.add(nums2[j]);
set.remove(nums2[j]);
}
}
// Convert ArrayList to array
int[] arr = new int[res.size()];
for (int i= 0; i < res.size(); i++) arr[i] = res.get(i);
return arr;
}
}
Java: Hashset T: O(n)
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Set<Integer> intersect = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
set.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i++) {
if (set.contains(nums2[i])) {
intersect.add(nums2[i]);
}
}
int[] result = new int[intersect.size()];
int i = 0;
for (Integer num : intersect) {
result[i++] = num;
}
return result;
}
}
Java: two points, T: O(nlogn)
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
while (i < nums1.length && j < nums2.length) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
set.add(nums1[i]);
i++;
j++;
}
}
int[] result = new int[set.size()];
int k = 0;
for (Integer num : set) {
result[k++] = num;
}
return result;
}
}
Java: Binary Search, T: O(nlogn)
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Arrays.sort(nums2);
for (Integer num : nums1) {
if (binarySearch(nums2, num)) {
set.add(num);
}
}
int i = 0;
int[] result = new int[set.size()];
for (Integer num : set) {
result[i++] = num;
}
return result;
} public boolean binarySearch(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
return true;
}
if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
}
Python:
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
res = []
s = set()
for num in nums1:
s.add(num) for num in nums2:
if num in s:
res.append(num)
s.remove(num) return res
Python:
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1=set(nums1)
nums2=set(nums2)
return list(nums1&nums2)
C++:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
set<int> s(nums1.begin(), nums1.end()), res;
for (auto a : nums2) {
if (s.count(a)) res.insert(a);
}
return vector<int>(res.begin(), res.end());
}
};
C++:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
int i = 0, j = 0;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) ++i;
else if (nums1[i] > nums2[j]) ++j;
else {
if (res.empty() || res.back() != nums1[i]) {
res.push_back(nums1[i]);
}
++i; ++j;
}
}
return res;
}
};
类似题目:
[LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
[LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
All LeetCode Questions List 题目汇总
[LeetCode] 349. Intersection of Two Arrays 两个数组相交的更多相关文章
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 349 Intersection of Two Arrays 两个数组的交集
给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示: 每个在结果中的元素必定是唯一的. 我们 ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- [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]349. Intersection of Two Arrays数组交集
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
随机推荐
- wpscan 更新超时报错
wpscan更新超时报错 本人亲测方法2 https://data.wpscan.org/plugins.json https://data.wpscan.org/plugins.json.sha51 ...
- 17、Learning and Transferring IDs Representation in E-commerce笔记
一.摘要 电子商务场景:主要组成部分(用户ID.商品ID.产品ID.商店ID.品牌ID.类别ID等) 传统的编码两个缺陷:如onehot,(1)存在稀疏性问题,维度高(2)不能反映关系,以两个不同的i ...
- zookeeper题目
1. ZooKeeper是什么?2. ZooKeeper提供了什么?3. Zookeeper文件系统4. ZAB协议?5. 四种类型的数据节点 Znode6. Zookeeper Watcher 机制 ...
- 使用selenium三种方式打开文件:
#路径读取方式一:# b.get(r"C:\我的代码\selenium自动化测试\test.html")#路径读取方式二:# b.get("C:\\我的代码\\selen ...
- django-使用类视图
视图函数views.py中 from django.shortcuts import render, redirect from django.http import HttpResponse, Js ...
- jeecg uedit 自定义图片上传路径
jeecg uedit 图片上传配置自定义物理路径,简单描述:我们知道 jeecg 中使用的 uedit 默认图片上传路径为 "当前项目\plug-in\ueditor\jsp\upload ...
- OLED液晶屏幕(1)OLED液晶屏幕ssd1306驱动芯片 arduino运行 ESP8266-07可以 12f不可以
OLED屏幕有各种形状和尺寸,但目前有两种非常受欢迎的屏幕尺寸. 1)0.96“ 2)1.3“ 他们也有2种常见的颜色 1)蓝色 2)白色 驱动OLED的芯片常用的有两种.这两种芯片有许多非常相似的设 ...
- Redis的通用key操作
这些操作跟具体的类型没有关系,而是跟key相关. 1.查询Redis中的key的名称: 所有key: 以my开头: 2.删除键: 3.判断某一个键是否存在: 4.重命名: 5.设置过期时间: 如果未设 ...
- Using HAProxy as an API Gateway, Part 1 [Introduction]
转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-1/ An API gateway handles load ...
- 括号匹配(POJ2955)题解
原题地址:http://poj.org/problem?id=2955 题目大意:给出一串括号,求其中的最大匹配数. 我这么一说题目大意估计很多人就蒙了,其实我看到最开始的时候也是很蒙的.这里就来解释 ...