mycode  77.78%

class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
def deal(dic,nums):
res = []
for item in nums:
if item in dic and dic[item]!=0:
res.append(item)
dic[item] -= 1
return res dic = {}
if len(nums1) > len(nums2):
for item in nums2:
dic[item] = dic.get(item,0) + 1
return deal(dic,nums1)
else:
for item in nums1:
dic[item] = dic.get(item,0) + 1
return deal(dic,nums2)

参考:

1、应用collection.Counter库,可以实现与运算

class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list((collections.Counter(nums1) & collections.Counter(nums2)).elements())

2、

class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
res = []
map = {}
for i in nums1:
map[i] = map[i]+1 if i in map else 1
for j in nums2:
if j in map and map[j] > 0:
res.append(j)
map[j] -= 1 return res

leetcode-easy-array-50. Intersection of Two Arrays II的更多相关文章

  1. [LeetCode&Python] Problem 350. Intersection of Two Arrays II

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

  2. 【leetcode❤python】350. Intersection of Two Arrays II

    #-*- coding: UTF-8 -*- class Solution(object):    def intersect(self, nums1, nums2):                ...

  3. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  4. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  5. 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 ...

  6. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  7. LeetCode_350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...

  8. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  9. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

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

  10. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

随机推荐

  1. localStorage 理解

    localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一种协议,在同一个端口上. 过期策略: ...

  2. ABAP中TAB分隔符的使用

    在ABAP开发中,存在很多特殊字符,使用情况也不同,下面及时SAP中的TAB分隔符的使用案例: 46C以下的版本: DATA: gc_result(50) type c. constants: con ...

  3. Python回调函数用法实例

    Python回调函数用法实例 作者:no.body链接:https://www.zhihu.com/question/19801131/answer/27459821 什么是回调函数? 我们绕点远路来 ...

  4. LVS实现负载均衡原理及安装配置 负载均衡

    LVS实现负载均衡原理及安装配置 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F ...

  5. FMDB-FMDatabaseQueue

    FMDB封装了SQLite3的方法,操作数据库变得很简单. 增删改查变简单之后,那么问题来了,如何使用多线程优化对数据库的操作? 这是我们的第一反应估计是dispatch_async(). 那么问题又 ...

  6. 解决错误:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://shiro.apache.org/tags]

    服务器错误信息如下: 解决方法: 把shiro包中的tld文件(shiro.tld)解压出来放到WEB-INF文件夹下即解决问题. 参考:http://blog.sina.com.cn/s/blog_ ...

  7. Delphi7 IDE

  8. Linux基础命令练习题(附答案)

    1.分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容,并用自己的话总计出这三个文档操作命令的不同之处? [root@localhost ~]# cat ...

  9. u-boot v2018.01 启动流程分析 简单版(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_39655765/artic ...

  10. 前端每日实战:141# 视频演示如何用 CSS 的 Grid 布局创作一枚小狗邮票

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BOeEYV 可交互视频 此视频是可 ...