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. sql server 获取随机数函数RAND()和RAND(x)

    --RAND(x)返回一个随机浮点值v,范围在0~1之间(即0<=v<=1.0) --若指定一个整数参数x,则它被用作种子值,使用相同的种子数将产生重复序列.如果同一种子值多次调用RAND ...

  2. Mysql数据库中的输入命令各类知识总结

    1.链接数据库的命令---mysql-u root-p 回车,输入密码//在cmd上输入自己的账号密码 2.查看:show databases: 3.创建数据库:create +database+数据 ...

  3. vue实现轮播图

    /* Start  基本样式*/ * {   margin: 0;   padding: 0; } ul {   list-style-type: none; } body {   font-size ...

  4. AQtime使用

    今天刚到网上下了AQtime.因为有个通信及数据存储的程序出现的内存泄漏.在用户的环境里出现了两次,在测试的环境里一次也没有出现,开发人员猜测是一部分代码引起的.说要代码的覆盖测试.看看测试环境里有那 ...

  5. 2018.09.07 最新cocoapods安装流程

    这篇写在简书了,就不费力气搬了,给简书留一篇. https://www.jianshu.com/p/13bbbf804b71

  6. OneDrive高速下载链接分享

    目录 1. 下载帮助 2. 本文地址 3. 资源链接 4. 打赏&支持 5. 关于&联系我 1. 下载帮助 OneDrive下载教程,建议不了解的先看下: https://www.cn ...

  7. sed 删除用法

    sed ‘1,4d’ dataf1 #把第一行到第四行删除,并且显示剩下的内容 sed ‘/La/d’ dataf2 #把含有 La 的行删除 sed ‘/La/!d`’#把不含 La 的行删除,!是 ...

  8. Big Data(六)用户权限实操&HDFS-API实操

    创建用户实操 1.创建用户god useradd god passwd god 2.设置ssh免密 ssh-copy-id -i id_dsa node02 3.修改hdfs-site.xml中的ss ...

  9. python爬取百度图片

    import requests import re from urllib import parse import os from threading import Thread def downlo ...

  10. Luogu P3170 [CQOI2015]标识设计 状态压缩,轮廓线,插头DP,动态规划

    看到题目显然是插头\(dp\),但是\(n\)和\(m\)的范围似乎不是很小.我们先不考虑复杂度设一下状态试试: 一共有三个连通分量,我们按照\(1,2,3\)的顺序来表示一下.轮廓线上\(0\)代表 ...