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 ...
随机推荐
- 2019/7/22----tomacat配置web页面访问路径
tomcat----conf-----Catalina----localhost----cms.xml,cms.xml文件中添加: <?xml version='1.0' encoding=&q ...
- regedit系统注册表,msconfig系统配置
msconfig msconfig即系统配置实用程序,是Microsoft System Configuration的缩写.是在开始菜单里运行中输入然后确认就可以找到程序开启或者禁用, 可以帮助电脑禁 ...
- IntelliJ IDEA 2017 MySQL5 绿色版 Spring 4 Mybatis 3 配置步骤详解(二)
前言 继续上一篇安装教程 首先是MySQL绿色版安装之后其他组件安装,如果篇幅较长会分为多篇深入讲解,随笔属于学习笔记诸多错误还望指出 共同学习. MySQL 5.7 绿色版 我本地安装的是 ...
- hadoop错误记录部分总结
错误记录与分析 错误1:java.net.BindException: Port in use: localhost:0 datanode节点启动时报错 日志信息如下: Exiting with st ...
- 关于hexo与github使用过程中的问题与笔记
快速阅读 如何用github 和hexo 创建一个blog 1.github中要新建一个与用户名同一样的仓库, 如:homehe.github.io - 必须是io后缀.一个帐户 只能建立一个 2. ...
- 利用iterm2,在命令行预览图片,服务器也是可以的
1.首先你本地电脑上要安装iterm2软件,我们这里使用brew安装 这个是一定要装的,因为能在命令行渲染出图片文件全靠它,其实不是服务器渲染出来的,而是iterm2 官方网站:https://www ...
- IDEA_2019.2的安装与个人配置(Windows)
1. 下载 官方下载网站:https://www.jetbrains.com/idea/download/ IDEA是支持多平台的开发工具,分为Windows.Mac和Linux三个平台,这里就只拿W ...
- JavaScript 如何工作的: 事件循环和异步编程的崛起 + 5 个关于如何使用 async/await 编写更好的技巧
原文地址:How JavaScript works: Event loop and the rise of Async programming + 5 ways to better coding wi ...
- web前端兼容性问题
传送门:https://www.cnblogs.com/zhoudawei/p/7497544.html
- 【转】JDK5.0中JVM堆模型、GC垃圾收集详细解析
基本概念 堆/Heap JVM管理的内存叫堆:在32Bit操作系统上有4G的限制,一般来说Windows下为2G,而Linux下为3G:64Bit的就没有这个限制.JVM初始分配的内存由-Xms指定, ...