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 ...
随机推荐
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- 文件操作2-Day3
一.文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 1.普通打开模式 r:只读模式(不加参数则默认只读:不能写,不能追加) w:只写模式(只能 ...
- 获取当前主题颜色 Flutter
通过context获取当前主题颜色 Theme.of(context).accentColor
- asp.net之大文件分段上传、断点续传
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- webuploader+php如何实现分片+断点续传
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- 转 Storm JAVA_HOME is incorrectly set.
问题可能有两个原因: 1.在环境变量中未设置JAVA_HOME变量名称. 解决办法: 在环境变量中添加. 或者在storm中的bin文件下有一个storm-config.cmd,使用文本打开,查询JA ...
- IntelliJ IDEA 2017 Dubbo Elastic-job Redis Zookeeper RabbitMQ FastDFS MyCat 简介以及部分实现(三)
前言 首先需要说明一下,与前两章的安装篇不太一样,这篇主要扫清一下这些插件/框架 等都是干什么用的,大多数都会用于服务端或监测工具或其他,作为新手建立一个大概的思想更好的了解自己的项目.废话不多 ...
- Python逆向(四)—— Python内置模块dis.py源码详解
一.前言 上一节我们对Python编译及反汇编做了讲解,大家知道dis模块可以将编译好的pyc文件中提取出来的PyCodeObject反汇编为可以阅读字节码形式.本节我们对dis模块中的源码进行详细的 ...
- java如何实现多线程?线程的状态有哪些?
java实现多线程有两种方法 1.继承Thread类 2.实现Runnable接口 这两种方法的共同点: 不论用哪种方法,都必须用Thread(如果是Thead子类就用它本身) ...
- namenode 优化 mv慢的问题
问题现象 问题描述 公司业务程序需求每30分钟mv 一万多个文件,如果三十分钟之内当前的文件内容没有全部移动,程序报错并且停止. 分析 通过分析,发现在启动balancer和不启动balancer的情 ...