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 ...
随机推荐
- python不使用系统库中的排序方法判断一个数组是否是有序数组
2. 给定一组整数, 已知其每两个数都互不相同,判断这些数字是否能排成一个有序的数组? 例:li = [1,3,4,2] 是有续的 可以排序为li =[1,2,3,4] li = [2,4,6,8] ...
- 2019/8/20 Test
题目 简述 做法 \(BSOJ2237\) 求\(\displaystyle{k\in G:\sum_{i\in G\vee j\in G}\frac{C^k_{i,j}}{C_{i,j}}}\),其 ...
- learning java AWT 布局管理器 GridLayout
GridLayout布局管理器将容器分割成纵横线分格的网格,每个网格所占的区域大小相同. import java.awt.*; public class GridLayoutTest { public ...
- C++命令空间使用和编译
先创建目录结构 src bin obj lib include 1.创建命名空间 创建一个头文件include/head.h; #ifndef _GOOD_H #define _GOOD_H name ...
- 【一起来烧脑】底层HTTP深入笔记
[外链图片转存失败(img-0GQ8QDNb-1563568792102)(https://upload-images.jianshu.io/upload_images/11158618-5bc7a2 ...
- ssm + ehcache 运行异常
异常: DEBUG [net.sf.ehcache.CacheManager@295af581] - Checking for update...DEBUG [net.sf.ehcache.Cache ...
- Fluent当中的通配符【翻译】
本文翻译自fluent帮助手册 可以在TUI当中使用通配符指定特定区域的名称,一些例子如下: 通配符*等同于"所有区域"例如: -/display/boundary-grid * ...
- kubernetes(K8S)创建自签TLS证书
TLS证书用于进行通信使用,组件需要证书关系如下: 组件 需要使用的证书 etcd ca.pem server.pem server-key.pem flannel ca.pem server.pem ...
- jvm 命令使用调优 通过jstat、jmap对java程序进行性能调优
转载:http://blog.csdn.net/jerry024/article/details/8507589 转载: https://blog.csdn.net/zhaozheng7758/art ...
- python : takes 0 positional arguments but 1 was given
def 的要加self, https://blog.csdn.net/u010269790/article/details/78834410