leetcode 349. Intersection of Two Arrays
来源:互联网 发布:java erp系统 编辑:程序博客网 时间:2024/06/14 00:46
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.
Subscribe to see which companies asked this question
思考:求两个数组的交集, 要求去重;
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> countSet = new HashSet<>();
Set<Integer> finalSet = new HashSet<>();
for(int num : nums1){
countSet.add(num);
}
for(int num : nums2){
if(countSet.contains(num)){
finalSet.add(num);
}
}
int[] ret = new int[finalSet.size()];
int i = 0;
for(int num : finalSet){
ret[i++] = num;
}
return ret;
}
}
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode题解 349. Intersection of Two Arrays
- LeetCode - 349. Intersection of Two Arrays
- [Leetcode]349. Intersection of Two Arrays
- 349. Intersection of Two Arrays--LeetCode Record
- LeetCode 349. Intersection of Two Arrays
- hdu 1988-More is better-带权并查集
- Android WebView第三方网站注入在线JS地址
- SVN环境搭建
- 如何在Unity中对程序进行 Android 真机断点调试?
- Sum Two
- leetcode 349. Intersection of Two Arrays
- 5-17 SQLite GreenDAO 笔记 待整理
- php连接数据库
- Struts2属性驱动与模型驱动
- Extjs框架总结
- 比较ArrayList、LinkedList、Vector
- linux debug开关 dev_dbg
- Android保存图片到图库,Android扫描文件到媒体库,Android保存图片到SD卡
- 真理-编程人员要注意的点