Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example

Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

分析:

这种考察一个数是否存在另一个集合中,一般来讲,都会用到HashSet. 如果不允许用额外的存储空间,可以对nums1排序,然后使用二分查找。

 public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
int[] arr = new int[];
return arr;
} Set<Integer> set = new HashSet<>();
for (int i : nums1) {
set.add(i);
} List<Integer> list = new ArrayList<>(); for (int i : nums2) {
if (set.contains(i)) {
list.add(i);
set.remove(i);
}
} return list.stream().mapToInt(i->i).toArray();
}

Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example

Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

这一次需要把所有的都找出来,可以用HashMap<Integer, Integer> 来存储,第一个Integer指的是数,第二个指的是那个数存在的个数。

 public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
int[] arr = new int[];
return arr;
} Map<Integer, Integer> map = new HashMap<>();
for (int i : nums1) {
map.put(i, map.getOrDefault(i, ) + );
} ArrayList<Integer> list = new ArrayList<>(); for (int i : nums2) {
if (map.containsKey(i) && map.get(i) >= ) {
list.add(i);
map.put(i, map.get(i) - );
}
}
return list.stream().mapToInt(i->i).toArray();
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

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. 26. leetcode 350. Intersection of Two Arrays II

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

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

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

  9. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

随机推荐

  1. Servlet获取简单验证码

    package com.helloweenvsfei.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Gra ...

  2. jsp笔记

    Jsp  Web服务器访问jsp的过程. 如果是第一次访问jsp文件,web服务器会把jsp翻译成一个servlet文件.再将其编译成一个.class文件.然后加载到内存.蓝色的地方也是为什么jav ...

  3. gitlab 配置邮箱

    摘自:http://www.tuicool.com/articles/ruyERz 这里我用的是263企业邮箱 这里我们用smtp发送邮件,要是系统自带有sendmail,我们就给他卸掉,yum re ...

  4. CSS_复习

    //这个可以作为补白居中的替补方法<!doctype html> <html> <head> <meta charset="utf-8"& ...

  5. sql-select

    SELECT 将资料从数据库中的表格内选出 指令 SELECT "栏位名" FROM "表格名"; 例如:查询Store_Information表中所有的的St ...

  6. java系统高并发解决方案(转载)

    转载博客地址:http://blog.csdn.net/zxl333/article/details/8454319 转载博客地址:http://blog.csdn.net/zxl333/articl ...

  7. Eclipse在线安装ADT插件

    要想使用Eclipse开发Android应用,首先要安装一个ADT插件,在此记录一下在Eclipse中采用在线安装的方式ADT插件,我使用的Eclipse版本是:eclipse-jee-luna-SR ...

  8. nl命令详解

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  9. Linux下SVN安装配置和使用中遇到的问题

    两个命令: svn info :显示版本库信息,svn的下载url等. svn co https://xxxxx/xxx   wodemulu   (通过我的目录制定co的文件夹) svn st:显示 ...

  10. Jquery 表单操作

    文本框,文本区域: 获取值: 1.$("#txt").attr("value"); 2. $("txt").val(); 单选按钮: 获取值 ...