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 ...
随机推荐
- oracle SQL 练习
COURSE 表 DROP TABLE "SCOTT"."course"; CREATE TABLE "SCOTT"."cours ...
- mybatis 多参数传递
参考: 1. MyBatis传入多个参数的问题 http://www.cnblogs.com/mingyue1818/p/3714162.html2. MyBatis报错 Parameter '0' ...
- 【PAT甲级】1017 Queueing at Bank (25 分)
题意: 输入两个正整数N,K(N<=10000,k<=100)分别表示用户的数量以及银行柜台的数量,接下来N行输入一个字符串(格式为HH:MM:SS)和一个正整数,分别表示一位用户到达银行 ...
- Android Dialog弹框提示
public void showUpdateDialog(String content) { //普通的AlertDialog对话框 AlertDialog.Builder builder = new ...
- linux搭建mysql时ifconfig命令无法使用问题
刚搭建好的Centos 最小安装模式是没有ifconfig命令的.改变步骤:一:使用语句:cd /etc/sysconfig/network-scripts/二:使用语句vi ifcfg-eno167 ...
- (转)notepad++去重
notepad++ 真是强大,几乎你能想到的处理文本方法都可以用它来实现,因为他有强大的插件团! 例如1:去除重复行 先安装TextFx插件 在菜单TextFX-->TextFX Tools下 ...
- free to monitor your sqlserver easy and safe and ...
Unlike AWR in Oracle, Sqlserver does not have offical way to make history performance information fo ...
- 「NOI2015」软件包管理器
题目描述 题面比较啰唆,我先把大体意思讲一下: 首先,有编号从\(0\)到\(N-1\)的\(N\)个节点,根节点一定是\(0\)号节点(无前驱) (我把下标都加上了一,转化为以\(1\)为起始下标的 ...
- FFmpeg笔记-基本使用
FFmpeg是目前最牛逼的开源跨平台音视频处理工具. 准备知识 我不是音视频编解码出身的,对于这一块非常的不了解,导致在学习FFmpeg的时候云里雾里的,所以学习之前最好看些资料对音视频编解码有点认识 ...
- js加密(十)csti.cn md5
1. http://www.csti.cn/index.htm 2. 登录密码加密 3. 加密js: var hexcase = 0; var b64pad = ""; var c ...