美文网首页
349. Intersection of Two Arrays

349. Intersection of Two Arrays

作者: jluemmmm | 来源:发表于2021-02-15 10:45 被阅读0次

求两个数组的交集。

两个集合

  • 时间复杂度 O(m + n)
  • 空间复杂度O(m + n)
  • Runtime: 76 ms, faster than 92.49%
  • Memory Usage: 40.8 MB, less than 15.49%
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function(nums1, nums2) {
    let set1 = new Set(nums1)
    let set2 = new Set(nums2)
    let res = []
    for(let e of set1) {
        if(set2.has(e)) {
            res.push(e)
        }
    }
    return res
};

相关文章

网友评论

      本文标题:349. Intersection of Two Arrays

      本文链接:https://www.haomeiwen.com/subject/lcgkxltx.html