LeetCode_349. Intersection of Two Arrays
349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Note:
- Each element in the result must be unique.
- The result can be in any order.
package leetcode.easy; public class IntersectionOfTwoArrays {
public int[] set_intersection(java.util.HashSet<Integer> set1, java.util.HashSet<Integer> set2) {
int[] output = new int[set1.size()];
int idx = 0;
for (Integer s : set1) {
if (set2.contains(s)) {
output[idx++] = s;
}
} return java.util.Arrays.copyOf(output, idx);
} public int[] intersection1(int[] nums1, int[] nums2) {
java.util.HashSet<Integer> set1 = new java.util.HashSet<Integer>();
for (Integer n : nums1) {
set1.add(n);
}
java.util.HashSet<Integer> set2 = new java.util.HashSet<Integer>();
for (Integer n : nums2) {
set2.add(n);
} if (set1.size() < set2.size()) {
return set_intersection(set1, set2);
} else {
return set_intersection(set2, set1);
}
} public int[] intersection2(int[] nums1, int[] nums2) {
java.util.HashSet<Integer> set1 = new java.util.HashSet<Integer>();
for (Integer n : nums1) {
set1.add(n);
}
java.util.HashSet<Integer> set2 = new java.util.HashSet<Integer>();
for (Integer n : nums2) {
set2.add(n);
} set1.retainAll(set2); int[] output = new int[set1.size()];
int idx = 0;
for (int s : set1) {
output[idx++] = s;
}
return output;
} @org.junit.Test
public void test1() {
int[] nums1 = { 1, 2, 2, 1 };
int[] nums2 = { 2, 2 };
int[] result = intersection1(nums1, nums2);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
result = intersection2(nums1, nums2);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
} @org.junit.Test
public void test2() {
int[] nums1 = { 4, 9, 5 };
int[] nums2 = { 9, 4, 9, 8, 4 };
int[] result = intersection1(nums1, nums2);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
result = intersection2(nums1, nums2);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
}
}
LeetCode_349. Intersection of Two Arrays的更多相关文章
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- Intersection of Two Arrays | & ||
Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
随机推荐
- VC中文件路径问题
环境:xp+vs2010 1.如果出现这样的路径,input.txt表示在解决方案目录(前提是项目包在解决方案目录下的一个包)下,也就是与你的解决方案XXX.sln平行. ifstream in(&q ...
- commons-dbutils工具栏的编写
db.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test_db?useUnicode=t ...
- django 第五天 自定义标签 静态文件
昨日忘记上传,先预留位置,稍后补上
- Processing 中玩增强现实 Argument Reality
其实2009年Processing就能做AR了,只是我不知道而已~ 需要以下几个东西: 1.JMyron 2.GSVideo 3.nyar4psg 4.Picking 5.OBJLoader 或者大伙 ...
- select2多选设置select多选,select2取值和赋值
select2设置select多选,select2取值和赋值,作为筛选条件的时候,取值相对简单,把选中的id值转为字符串传给后端查询,查询之后会刷新页面,为了在下拉框中显示刚刚选中的值,就需要给sel ...
- 震惊,hexo个人博客居然有这么方便的评论系统
论文搞得一头火,瞎倒腾了一下,没想到几分钟给自己的博客换了个评论系统. 之前用的gitalk,需要依赖github,死活没有成功,而且评论者还需要登录github才可以评论,不好用,刚才偶然间发现va ...
- HearthBuddy的class276以及class247
使用de4dot-cex反编译原版的hearthbuddy得到的 链接: https://pan.baidu.com/s/1hT79LpIjbyvODsjnkSe_5A 提取码: iemx class ...
- 狼人杀面杀APP(FGUI教程)
本教程为FairyGUI进阶教程,这是一套完整的面杀桌游APP.游戏不仅有发放随机身份的功能,还涉及10个页面的切换与各页面不同状态(最多达9种状态)的切换,众多复杂UI的交互,多语言切换,3D粒子在 ...
- 静态导入(static import)
1.传统静态方法的调用方式 定义一个Common类,里面有静态方法和静态常量 package com.example.common; public class Common { public stat ...
- [转]Git 代码撤销、回滚到任意版本(当误提代码到本地或master分支时)
两种情况(场景) 情况一 代码还只在本地,未push到运程仓库,想把代码还原到上一次commit的代码,此时操作为代码撤销 解决方案: 1 git reset [--hard|soft|mi ...