LeetCode 349. Intersection of Two Arrays(数组交集)
来源:互联网 发布:线刷宝刷机有垃圾软件 编辑:程序博客网 时间:2024/06/04 23:28
原题网址:https://leetcode.com/problems/intersection-of-two-arrays/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
方法:通过集合求交集。
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet<>(); Set<Integer> set2 = new HashSet<>(); for(int num: nums1) set1.add(num); for(int num: nums2) if (set1.contains(num)) set2.add(num); int[] result = new int[set2.size()]; int i=0; for(int num: set2) result[i++] = num; return result; }}
0 0
- LeetCode 349. Intersection of Two Arrays(数组交集)
- 349.leetcode Intersection of Two Arrays(easy)[数组 交集 set]
- [LeetCode]349. Intersection of Two Arrays(两数组的交集)
- leetcode 349. Intersection of Two Arrays 数组的交集
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- Leetcode :Intersection of Two Arrays 两个数组的交集
- (LeetCode)Intersection of Two Arrays --- 求交集
- 349. Intersection of Two Arrays 两个数组交集
- 349. Intersection of Two Arrays | 找两个数组的交集
- Intersection of Two Arrays II两个数组交集(重要!)
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- leetcode解题之349 & 350. Intersection of Two Arrays Java版(求数组交集))
- 349. Intersection of Two Arrays (求两个数组的交集,不包含重复元素)
- Leetcode 350. Intersection of Two Arrays II 数组交集2 解题报告
- [easy]【leetcode】【java】 Intersection of Two Arrays II 两个数组的交集
- leetcode-350 Intersection of Two Arrays II 求两个数组的交集 面试见到了吗?
- [LeetCode] Intersection of Two Arrays II 两个数组的交集II
- 啦啦啦啦~Django1.96---编写第一个Django应用(2)
- 如何使图片分辨率与清晰度满足SCI期刊的严格要求
- Sql中EXISTS与IN的效率问题
- kvm--虚拟机运行
- 详解Linux中的mv命令
- LeetCode 349. Intersection of Two Arrays(数组交集)
- linux命令执行返回值
- 第十二周第一项目——阅读程序
- 不使用字符串处理的库函数,实现strcpy
- C++ primerplus 第12章课本上例题
- ipyhon中 import pyspark
- android onNewIntent和使用通知时问题
- JavsScript与时间相关的函数
- 为什么要使用SLF4J而不是Log4J