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排序,然后使用二分查找。

  1. public class Solution {
  2. public int[] intersection(int[] nums1, int[] nums2) {
  3. if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
  4. int[] arr = new int[];
  5. return arr;
  6. }
  7.  
  8. Set<Integer> set = new HashSet<>();
  9. for (int i : nums1) {
  10. set.add(i);
  11. }
  12.  
  13. List<Integer> list = new ArrayList<>();
  14.  
  15. for (int i : nums2) {
  16. if (set.contains(i)) {
  17. list.add(i);
  18. set.remove(i);
  19. }
  20. }
  21.  
  22. return list.stream().mapToInt(i->i).toArray();
  23. }

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指的是数,第二个指的是那个数存在的个数。

  1. public class Solution {
  2. public int[] intersect(int[] nums1, int[] nums2) {
  3. if (nums1 == null || nums2 == null || nums1.length == || nums2.length == ) {
  4. int[] arr = new int[];
  5. return arr;
  6. }
  7.  
  8. Map<Integer, Integer> map = new HashMap<>();
  9. for (int i : nums1) {
  10. map.put(i, map.getOrDefault(i, ) + );
  11. }
  12.  
  13. ArrayList<Integer> list = new ArrayList<>();
  14.  
  15. for (int i : nums2) {
  16. if (map.containsKey(i) && map.get(i) >= ) {
  17. list.add(i);
  18. map.put(i, map.get(i) - );
  19. }
  20. }
  21. return list.stream().mapToInt(i->i).toArray();
  22. }
  23. }

转载请注明出处: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. Xunit

    Attributes Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated ...

  2. windows Server2008R2 每隔一段时间自动关机解决办法

    情况描述: “我的电脑-->右键属性”中显示“已激活”,而“管理工具”中显示未激活.系统中有进程wlms.exe. 网上找了下解决方式: 1.提权工具:PSTOOLS(下载地址:http://m ...

  3. Dinic问题

    问题:As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offic ...

  4. 【心得&&体会】

    ★2016.1.1★ 很早就想写这样的一篇blog了,但一直没有抽空去实现,新的一年感觉应该有所改变,故深夜提笔(码字) NOIP卡掉和连续两次月考爆炸,这段时间确实心理不舒服,调节的也不是很到位,但 ...

  5. BZOJ-1015 StarWar星球大战 并查集+离线处理

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Submit: 4105 Solved: 1826 [Submit ...

  6. 【poj2234】 Matches Game

    http://poj.org/problem?id=2234 (题目链接) 题意 经典取火柴游戏 Solution 裸的Nim游戏,也就是取石子. 整个游戏的sg值为每一堆火柴(子游戏)的异或和. 代 ...

  7. MVC4笔记 Area区域

    mvc4.0新增的area区域机制,可以协助你在架构较为大型的项目,让独立性较高的部分功能独立成一个MVC子网站,以降低网站与网站之间的耦合性,也可以通过area的切割,让多人同时开发同一个项目时候, ...

  8. mongo操作

    详细使用网址:http://blog.csdn.net/xinghebuluo/article/details/7050811 MongoDB基本使用 成功启动MongoDB后,再打开一个命令行窗口输 ...

  9. lnux下源码安装MySQL 5.6

    nux下源码安装MySQL 5.6 说明:本文是我自己测试的MySQL5.6源码安装,经本人亲自实践,完全可用,另在5.6之前的版本也是可以按照本文源码安装的.我是在两台linux下一台安装5.5,另 ...

  10. 让Windows 7内置Administrator 用户也能使用指纹登录

    前言 这周末重装了个系统,之前用windows 8 现在8.1预览版出来了,琢磨着是不是给升级玩玩.装上后感觉变化不大,后来一折腾,就换回windows 7 了(64位旗舰版).将安装时创建的用户删除 ...