[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 ...
随机推荐
- 通过Python实现mysql查询数据库实例
#coding:utf-8 ''' Created on 2017年10月25日 @author: li.liu ''' import pymysql db=pymysql.connect('loca ...
- 我理解的Linux内存管理
众所周知,内存管理是Linux内核中最基础,也是相当重要的部分.理解相关原理,不管是对内存的理解,还是对大家写用户态代码都很有帮助.很多书上.很多文章都写了相关内容,但个人总觉得内容太复杂,不是太容易 ...
- test20190901 NOI2019 模拟赛
0+0+0=0.还是太菜,看不出题型. Masodik 你要从 (0,0) 点走到 (n,m),每次只能往 x 轴或者 y 轴正方向移动一个单位距离.从 (i,j) 移动到 (i,j+1) 的代价为 ...
- solidworks 学习 (三)
汽车轮毂三维建模
- 使用vault pki engine 方便的管理证书
vault 是一个很方便的secret .敏感数据管理工具,当前的版本已经包含了UI,使用起来很方便 以下演示一个简单的pki 管理 项目使用docker-compose 运行,为了简单使用单机开发模 ...
- Angular2发送HTTP请求SpringBoot后台跨域问题解决
Angular通过http发送post请求至SpringBoot的Controller,由于同源策略的保护,遇到跨域问题: • 源(origin)就是协议(http).域名(localhost)和端口 ...
- 意图Intent
意图点击官方链接 前言 对意图Intent,学习安卓需掌握.以官方链接:http://www.android-doc.com/reference/android/content/Intent.html ...
- Android程序员问答题
前言 最近三个月内,不断地进行移动应用开发在线测试题,也积累了不一样的知识.这也将对android studio有很好的掌握,对将来面试也很有好处.那么我就分享给大家.分享是一种幸福,这是一种质的飞越 ...
- 使用ADO.NET
Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Linux 上配置 SQL Server Always On Availability Group
SQL Server Always On Availability Group 配置步骤:配置三台 Linux 集群节点创建 Availability Group配置 Cluster Resource ...