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

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

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

两种思路,一种是map,一种是排序归并的思想:

class Solution(object):def intersection(self, nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: List[int]"""#return list(set(nums1) & set(nums2))
        nums1.sort()nums2.sort()i = j = 0arr = set()while i<len(nums1) and j<len(nums2):if nums1[i] == nums2[j]:arr.add(nums1[i])i += 1j += 1elif nums1[i] > nums2[j]:j += 1else:i += 1return list(arr)

转载于:https://www.cnblogs.com/bonelee/p/8643772.html

leetcode 349. Intersection of Two Arrays相关推荐

  1. leetcode 349. Intersection of Two Arrays 1

    Description Given two arrays, write a function to compute their intersection. Example: Given nums1 = ...

  2. LeetCode 350. Intersection of Two Arrays II

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

  3. LeetCode之Intersection of Two Arrays

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

  4. 349. Intersection of Two Arrays 两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集.   示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...

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

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

  6. LeetCode -349 两个数组的交集

    难度:简单 给定两个数组 nums1 和 nums2 ,返回它们的交集 .输出结果中的每个元素一定是唯一 的.我们可以不考虑输出结果的顺序 . 题目链接 LeetCode -349 两个数组的交集 S ...

  7. 53 两数组的交集(Intersection of Two Arrays)

    文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...

  8. Leetcode: Intersection of Two Arrays

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

  9. LeetCode Intersection of Two Arrays

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

最新文章

  1. 【数据库(二)】嵌套子查询
  2. 解决IE不支持Data.parse()的问题
  3. HP大中华区总裁孙振耀谈工作、职业与人生规划
  4. 剖析java中的String之__拼接
  5. netty实现高性能文件服务器,通用文件服务组件(Netty实现版本)
  6. CountDownLacth详解
  7. leetcode - 279. 完全平方数
  8. BZOJ2137: submultiple(生成函数,二项式定理)
  9. 中国风吉祥纹样底纹背景,艾草绿和天青色趋势色彩
  10. final关键字_夯实基础:Java中final关键字的几种用法
  11. java 开发微信中回调验证一直提示 解密失败处理(Java)
  12. siob执行多条sql写法及创建表添加字段
  13. java sleep方法_6种快速统计代码执行时间的方法,真香!(史上最全)
  14. iscsi实现多路径
  15. 防火墙基本应用(华为USG6000V)
  16. 京东云 linux无法远程,怎样远程登录京东云云主机.pdf
  17. js 安卓和ios的一些奇奇怪怪的注意点
  18. 职业生涯手记——序章
  19. ThinkPHP6 excel 导出功能完整实现
  20. ardupilot/arduplane/attitude.cpp 姿态控制解析

热门文章

  1. JAVA之JVM知识汇总
  2. 关于LDD3 setconsole.c Alesssandro Rubini 的邮件回复
  3. php云人才系统调用,PHP云人才系统3.0正式发布
  4. 节点部署_国际电联设定大星座部署节点要求
  5. python阶乘匿名函数_python的高阶函数与匿名函数
  6. 每天一个linux命令(17):whereis 命令
  7. Java自学!Java项目面试介绍
  8. 【408预推免复习】操作系统引论
  9. MySQL_Workbench使用
  10. 二叉树求深度的递归的详细分析