leetcode349 350 Intersection of Two Arrays & II
"""
Intersection of Two Arrays
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]
"""
"""
由于问题中的元素是唯一的,所以我们只关心元素的有无,
那么我们可以使用set这个结构。
首先将nums1的所有数据存入set中,查找nums2中的数据是否在这个set中,
如果在的话,我们将这个元素存入一个list里面。
"""
class Solution:
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1 = set(nums1)
result = set()
for i in nums2:
if i in nums1:
result.add(i)
return list(result) """
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
"""
"""
发现在leetcode349这个问题的基础上,
我们多加了记录元素个数的功能,我们可以使用dict实现它。
"""
class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
record, result = {}, []
for num in nums1:
record[num] = record.get(num, 0) + 1
# dict.get(key, default=None)返回指定键的值,如果值不在字典中返回默认值None
#{1: 2, 2: 2}
for num in nums2:
if num in record and record[num]:
# num in record 里而且 对应有重复值
result.append(num)
record[num] -= 1
return result
leetcode349 350 Intersection of Two Arrays & II的更多相关文章
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- (Collection)350. Intersection of Two Arrays II
/* Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2 ...
随机推荐
- Windows驱动开发-IRP超时处理
IRP被送到底层驱动程序以后,由于硬件设备的问题,IRP不能得到及时处理,甚至有可能永远不会被处理,这时候需要对IRP超时情况进行处理,一旦在规定时间内,IRP没有被处理,操作系统就会进入到IRP的处 ...
- STM32的程序升级
IAP基础参考http://www.eeworld.com.cn/mcu/2018/ic-news112042038.html https://blog.csdn.net/tq384998430/ar ...
- [POI 2014]PTA-Little Bird
Description 题库连接 给你 \(n\) 棵树,第 \(i\) 棵树的高度为 \(d_i\).有一只鸟从 1 号树出发,每次飞跃不能超过 \(k\) 的距离.若飞到下一棵树的高度大于等于这一 ...
- 「BJOI2018」求和
「BJOI2018」求和 传送门 观察到 \(k\) 很小而且模数不会变,所以我们直接预处理 \(k\) 取所有值时树上前缀答案,查询的时候差分一下即可. 参考代码: #include <alg ...
- 「NOI2015」软件包管理器
题目描述 题面比较啰唆,我先把大体意思讲一下: 首先,有编号从\(0\)到\(N-1\)的\(N\)个节点,根节点一定是\(0\)号节点(无前驱) (我把下标都加上了一,转化为以\(1\)为起始下标的 ...
- yum软件仓库常用命令
一.PRM简化安装软件的复杂度 安装软件的命令格式 prm -ivh filename.rpm 升级软件的命令格式 prm -Uvh filename.rpm 卸载软件的命令格式 prm -e fil ...
- rundll32.exe文件详解
平时很常听到有些朋友说:呀,我系统的注册表启动项目有rundll32.exe,系统进程也有rundll32.exe,是不是病毒呀?这是对rundll32.exe接口不了解,其实其原理非常简单,了解并掌 ...
- runas的替代品CPAU使用
runas替代软件CPAU 在windows系统下,想要实现某个程序不论何时都以指定的用户身份登录,因此找到了CPAU这个软件 cpau官方网站:https://www.joeware.net/fre ...
- 如何创建Github账号及将本地项目上传至GitHub?
如何将本地项目上传至GitHub 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直 ...
- helm基本用法
一.helm命令 helm search #关键字搜索charts helm pull #压缩下载chart到本地,可以使用--untar下载解压) helm install #部署chart到kub ...