leetcode 349. 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]
.
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相关推荐
- leetcode 349. Intersection of Two Arrays 1
Description Given two arrays, write a function to compute their intersection. Example: Given nums1 = ...
- LeetCode 350. Intersection of Two Arrays II
题目: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- LeetCode之Intersection of Two Arrays
1.题目 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- 349. Intersection of Two Arrays 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- LeetCode -349 两个数组的交集
难度:简单 给定两个数组 nums1 和 nums2 ,返回它们的交集 .输出结果中的每个元素一定是唯一 的.我们可以不考虑输出结果的顺序 . 题目链接 LeetCode -349 两个数组的交集 S ...
- 53 两数组的交集(Intersection of Two Arrays)
文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 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
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
最新文章
- 【数据库(二)】嵌套子查询
- 解决IE不支持Data.parse()的问题
- HP大中华区总裁孙振耀谈工作、职业与人生规划
- 剖析java中的String之__拼接
- netty实现高性能文件服务器,通用文件服务组件(Netty实现版本)
- CountDownLacth详解
- leetcode - 279. 完全平方数
- BZOJ2137: submultiple(生成函数,二项式定理)
- 中国风吉祥纹样底纹背景,艾草绿和天青色趋势色彩
- final关键字_夯实基础:Java中final关键字的几种用法
- java 开发微信中回调验证一直提示 解密失败处理(Java)
- siob执行多条sql写法及创建表添加字段
- java sleep方法_6种快速统计代码执行时间的方法,真香!(史上最全)
- iscsi实现多路径
- 防火墙基本应用(华为USG6000V)
- 京东云 linux无法远程,怎样远程登录京东云云主机.pdf
- js 安卓和ios的一些奇奇怪怪的注意点
- 职业生涯手记——序章
- ThinkPHP6 excel 导出功能完整实现
- ardupilot/arduplane/attitude.cpp 姿态控制解析