You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var intersection = function(nums1, nums2) {
let map1 = new Set(nums1);
let map2 = new Set(nums2);
let res = []
map1.forEach((item) => {
if(map2.has(item)){
res.push(item)
}
})
return res
};
functionintersection(arr1,arr2){if(arr1instanceofArray&&arr2instanceofArray){constset=newSet(arr2);returnArray.from(newSet(arr1.filter(item=>set.has(item))));}throw'What I need are two arrays';}
Activity
finghtingjie commentedon Apr 6, 2020
const fn = (n1,n2)=>[...new Set(n1.filter(i => n2.includes(i)))]; console.log(fn([1,2,2,1],[2,2]))ai977313677 commentedon Apr 6, 2020
用Map是O(n)的时间
zhdxmw commentedon Apr 6, 2020
liwuhou commentedon Apr 6, 2020
Aiyoweioo commentedon Apr 6, 2020
000Sonic commentedon Apr 6, 2020
liwuhou commentedon Apr 6, 2020
这个好像不满足
000Sonic commentedon Apr 6, 2020
改了,没注意去重
uuRRx0 commentedon Apr 6, 2020
sisterAn commentedon Apr 6, 2020
解题思路:
filter过滤Set去重代码实现:
leetcode
hh-cheng commentedon Apr 6, 2020
JerryWen1994 commentedon Apr 7, 2020
@WeCheung 你这filter里面,每一次都要new Set一遍,虽然代码简写了,但浪费了很多资源
hh-cheng commentedon Apr 7, 2020
@597796340 多谢提醒
lyllovelemon commentedon Apr 8, 2020
[-]leetcode:给定两个数组,编写一个函数来计算它们的交集[/-][+]leetcode349:给定两个数组,编写一个函数来计算它们的交集[/+]4 remaining items
zero9527 commentedon Apr 28, 2020
consoles commentedon May 2, 2020
去重很简单,第二个for循环判断map中存在元素之后,ans的push之后将其删除即可
huangzhuangjia commentedon May 5, 2020
ruochuan12 commentedon May 14, 2020
Accepted
Been101 commentedon Jul 11, 2020
baebae996 commentedon Dec 3, 2020
这样的写法会导致最终的ans数组包含重复元素噢
[-]leetcode349:给定两个数组,编写一个函数来计算它们的交集[/-][+]腾讯&leetcode:给定两个数组,编写一个函数来计算它们的交集[/+]azl397985856 commentedon Mar 3, 2021
题目地址(349. 两个数组的交集)
https://leetcode-cn.com/problems/intersection-of-two-arrays/
题目描述
前置知识
公司
思路
先遍历第一个数组,将其存到 hashtable 中,然后遍历第二个数组,如果在 hashtable 中存在就 push 到 ret,然后清空 hashtable,最后返回 ret 即可。
关键点解析
代码
代码支持:JS, Python
Javascript Code:
Python Code:
复杂度分析
更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
JohnApache commentedon Apr 26, 2021
你这个有问题啊,已经推进去的元素,会出现重复的元素,我改了一下,多加一个map 存储已经推进去的元素即可避免重复
ninenan commentedon Aug 18, 2021
RyanMeg123 commentedon May 23, 2022
RainyNight9 commentedon Sep 27, 2023
lyllovelemon commentedon Sep 27, 2023