Leetcode:349. 两个数组的交集

跟着carl学算法,本系列博客仅做个人记录,建议大家都去看carl本人的博客,写的真的很好的!
代码随想录
Leetcode:349. 两个数组的交集

给定两个数组 nums1 和 nums2 ,返回 它们的 交集。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

示例 1:

输入:nums1 = 1,2,2,1, nums2 = 2,2

输出:2

示例 2:

输入:nums1 = 4,9,5, nums2 = 9,4,9,8,4

输出:9,4

解释:4,9 也是可通过的

java 复制代码
	public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        for(int i = 0; i < nums1.length; i++){
            set.add(nums1[i]);
        }
        Set<Integer> res = new HashSet<>();
        for(int j = 0; j < nums2.length; j++){
            if(set.contains(nums2[j])){
                res.add(nums2[j]);
            }
        }
        return res.stream().mapToInt(x -> x).toArray();
    }
相关推荐
JieE2121 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup112 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi002 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵2 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf2 天前
Agent 流程编排
后端·python·agent
copyer_xyf2 天前
Agent RAG
后端·python·agent
copyer_xyf2 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf2 天前
Agent 记忆管理
后端·python·agent