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 ...
随机推荐
- 2 JavaScript输出&字面量&变量&操作符&语句&标识符和关键字&字符集&语句&数据类型与类型转换
JS输出: JavaScript没有任何打印或者输出的函数,但是可以用不同的方式输出数据 window.alert():弹出警告框 document.write():写入文档 innerHTML:写入 ...
- 【转载】Oracle sqlplus中最简单的一些命令,设置显示的格式
登录数据库: 方式(1)当我们刚安装Oracle数据库时,登录账户时可以使用win+r 输入sqlplus,进入sqlplus命令窗口,然后输入用户名和密码,这里输入密码时不会有回显 方式(2)使用w ...
- TensorFlow基础三(Scope)
用到变量名了,就涉及到了名字域的概念.通过不同的域来区别变量名,毕竟给所有变量都直接取不同名字还是有点辛苦的. 主要是name_scope和variable_scope,name_scope 作用于操 ...
- R语言 table()函数
table函数 用 table() 函数统计因子各水平的出现次数(称为频数或频率).也可以对一般的向量统计每个不同元素的出现次数.如 sex = c("女","女&quo ...
- jsoup教学系列
http://my.oschina.net/flashsword/blog?catalog=390084
- 笔记||Python3进阶之调用外部程序
像wget可以下载文件 ffmpeg可以切割.合并.转换.录制视频 free命令可以查看linux内存使用信息 python提供了库来调用外部程序.命令?> 最常见的两种方法: ①o ...
- Java程序员所需要掌握的核心知识
[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. https://javaguide.cn/ 推荐使用 https://snailclimb.gitee.io/javag ...
- 远程服务器使用tensorboard
1 .由于服务器上tensorboard使用的端口是6006,因此,连接ssh时,将服务器的6006端口重定向到自己机器上的16006端口: ssh -L 16006:127.0.0.1:6006 u ...
- Spring--@configuration 和 @Bean
参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html @Configuration 和 @Bean 注 ...
- Linux中{ }的用法
一.生成序列 格式:{#..#},按照ASCII表的顺序进行生成,如{a..c}表示a b c,也可以{c..a}倒叙的形式生成c b a # ..} # echo {z..a} z y x w v ...