leetcode-easy-array-50. Intersection of Two Arrays II
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的更多相关文章
- [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 ...
- 【leetcode❤python】350. Intersection of Two Arrays II
#-*- coding: UTF-8 -*- class Solution(object): def intersect(self, nums1, nums2): ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 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
350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
随机推荐
- MySQL水平分表
业务表增长速度较快,单表数据较大,对表的读写有影响. 思路:化整为零,把单表拆解为多表,按指定的算法规则选择表. 好处:能大幅降低单表的数据,读写更快,同时分散了表数据, SQL语句也分散到不同的表中 ...
- IE6兼容笔记
1.IE6中,元素右浮动的时候前面不能有文本或内联元素,否则会换行独占一行 解决办法:将浮动元素放到文本或内联元素前面,大都在制作新闻列表的时候会遇到这种问题. 未完,待续!
- 一、Signalr WebApi客服
一.搭建环境 (redis服务) 链接测试 二.项目搭建 参考 1.搭建项目(直接项目-不包含MVC以及API) 项目结构 但是需要访问(所以还需要添加控制器Api的模式)选择Api 添加类库一个专门 ...
- new、virtual、override
我们先看下面一段程序: public class Father { public void Run0() { Console.WriteLine("Father.Run0"); } ...
- Linux抓包与扫描工具
一.nmap扫描工具介绍: 1.安装nmap,如下: 2.检查目标主机所开启的TCP服务: 3.检查x.x.x.x/24网段内哪些主机开启了FTP.SSH服务 二.使用tcpdump分析 1.执行FT ...
- Django学习系列6:使用selenium测试用户交互
学习系列5中的单元测试有报错信息,这儿来编写functional_tests.py文件,扩充其中的功能测试 # File: functional_test.py # Author: Rxf # Cre ...
- Python修炼之路-异常
异常处理 在程序出现bug时一般不会将错误信息直接显示给用户,而是可以自定义显示内容或处理. 常见异常 AttributeError # 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性 ...
- 微信小程序-自制弹出框禁止页面上下滑动
弹出 fixed 弹窗后,在弹窗上滑动会导致下层的页面一起跟着滚动. 解决方法: 在弹出层加上 catchtouchmove 事件 两种方法:(在电脑上测试是没有用的,这是触摸事件.因此,需要在手机端 ...
- vue 解决axios 跨域问题
闲着没事,假期敲vue 请求数据,总结下vue跨越问题 第一种.服务器服务器不支持跨域请求 1.当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev:{}部分. ...
- 'gbk' codec can't decode byte 0xad in position 12: illegal multibyte sequence
原文链接:https://blog.csdn.net/shijing_0214/article/details/51971734 使用python的时候,经常会遇到文本编码的问题,其中最常见的就是“' ...