349. Intersection of Two Arrays

Easy

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的更多相关文章

  1. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  2. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

  4. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

  5. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  6. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  7. Intersection of Two Arrays | & ||

    Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...

  8. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  9. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

随机推荐

  1. 分段三次Hermite插值及其与三次样条的比较

    分段三次 Hermite 插值多项式 (PCHIP) 语法 p = pchip(x,y,xq) pp = pchip(x,y)   说明 p = pchip(x,y,xq) 返回与 xq 中的查询点对 ...

  2. learning java AWT 手绘窗口

    import java.awt.*;port java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import jav ...

  3. 13、SparkContext详解

    一.SparkContext原理 1.图解 二.SparkContext源码 1.TaskScheduler创建 ###SparkContext.scala // Create and start t ...

  4. 【微信小程序】如何获取用户绑定手机号

    用户调用wx.login()方法,获取登录用户凭证code wx.login({ success: function(res) { console.log('loginCode', res.code) ...

  5. HDU-盐水的故事

    http://acm.hdu.edu.cn/showproblem.php?pid=1408 这是一道高精度问题: 在自己错了数十遍之后找到了不少规律: 首先是Output limit exceede ...

  6. BurpSuite经常拦截firefox报文如success.txt的解决办法

    因为工作需要经常使用Burp对收发报文进行检测,平时习惯使用火狐浏览器,但是火狐浏览器经常进行一些登录状态的检测,导致Burp拦截中出现大量的火狐报文,如http://detectportal.fir ...

  7. Spring boot POST 提交错误 Request header is too large

    解决方法 application.yml server: # 单位 KB max-http-header-size: 100000 java.lang.IllegalArgumentException ...

  8. Oracle语法 及 SQL题目(三)

    目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...

  9. Android反编译,apk反编译技术总结

    1.谷歌提供的工具:android-classyshark 下载地址:https://github.com/google/android-classyshark/releases,下载下来之后是一个可 ...

  10. GROUP_CONCAT 将mysql多条数据合并为一条

    实现将多条数据合并为一条数据,在mysql中可以通过 GROUP_CONCAT 函数实现 上面是潇leader发我的和工作不相关的小小小需求描述,很明显是要把id和name相同的数据合并为一条,下面按 ...