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. 083、Prometheus架构(2019-05-05 周日)

    参考https://www.cnblogs.com/CloudMan6/p/7692765.html   Prometheus 是一个非常优秀的监控工具,准确的说,应该是监控方案.Prometheus ...

  2. webpack常用的插件

    webpack常用的开发插件 1.clean-webpack-plugin 运行webpack build时先把打包进入的文件夹清空 注意,它是以对象的方式去接收的 const { CleanWebp ...

  3. hadoop最简伪分布式安装

    本次安装运行过程使用的是Ubuntu16.04 64位+Hadoop2.5.2+jdk1.7.0_75 Notice: Hadoop2.5.2版本默认只支持64位系统 使用的jdk可以为1.7和1.8 ...

  4. zabbix 内存溢出

    tail -f /var/log/zabbix/zabbix_server_log ::165110.914 ================================ ::165110.914 ...

  5. win10将mongodb加入系统服务,官方源码报错问题记录

    进入C:\Program Files\MongoDB\Server\3.6目录下 1.编写配置文件mongodb.cfg: dbpath=D:\MongoDB\data\db #数据库路径 logpa ...

  6. faiss的简单使用

    简介 faiss是为稠密向量提供高效相似度搜索和聚类的框架.由Facebook AI Research研发. 具有以下特性. 1.提供多种检索方法 2.速度快 3.可存在内存和磁盘中 4.C++实现, ...

  7. Linux文件命名规则

    Linux目录结构命名规定 几乎所有的Linux版本都会遵循FHS(Filesystem Hierarchy Standard),中文翻译过来即为文件系统层次化标准.类似于Windows操作系统中c盘 ...

  8. Python修炼之路-模块

    模块 模块与包 模块:用来从逻辑上组织python代码(可以定义变量.函数.类.逻辑:实现一个功能),本质就是.py结尾的python文件. 例如,文件名:test.py,对应的模块名为:test 包 ...

  9. Console Add Item –Java使用eBay API SDK刊登商品 详解

    准备工作: 1. 运行Eclipse (或其他Java IDE) 2.创建一个ConsoleAddItem工程(项目) 选JDK 1.5.1.6.1.8等版本,已测试1.6.1.8版本. 3.下载JA ...

  10. Rsync服务端部署流程

    Rsync服务端部署流程       Rsync服务端部署流程: 一.rsync服务端配置流程 配置rsync配置文件/etc/rsyncd.conf 创建同步的本地目录/dingjian 并根据需要 ...