【leetcode❤python】350. Intersection of Two Arrays II
#-*- coding: UTF-8 -*-
class Solution(object):
def intersect(self, nums1, nums2):
if len(nums1)<len(nums2):
tmp=nums1
nums1=nums2
nums2=tmp
tmpdic={}
for num in nums1:
tmpdic[num]=tmpdic[num]+1 if num in tmpdic else 1
commonList=[]
for num in nums2:
if tmpdic.get(num)>None and tmpdic.get(num)>=1:
commonList.append(num)
tmpdic[num]=tmpdic.get(num)-1
return commonList
sol=Solution()
print sol.intersect(nums1=[1,1,1], nums2=[1,1])
【leetcode❤python】350. 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】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】350. Intersection of Two Arrays II 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Java排序+双指针 Python排序+双指针 Python解 ...
- 【leetcode❤python】 160. Intersection of Two Linked Lists
#-*- coding: UTF-8 -*- #两种方法#方法1:#计算出A和B两个链表的长度分别为m.n;#长度长的链表先走m-n步,之后再一次遍历寻找#方法2:#先走到一个链表的尾部,从尾部开始走 ...
- [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❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
随机推荐
- js构造函数和继承实现方式
- 夺命雷公狗---node.js---14之DNS
node下如果想域名解析是需要通过apache或者ng的反向版定才可以实现的,但是他也给我们留下了哟套DNS操作方法: /** * Created by leigood on 2016/8/30. * ...
- SQL内连接与外连接的区别【转】
--表stuid name 1, Jack2, Tom3, Kity4, nono--表examid grade1, 562, 7611, 89 内连接 (显示两表id匹配的)select stu.i ...
- URL 中#号,? ,&的作用 (摘抄整理 链接为学习地址)
1. 一峰的网络日志:http://www.ruanyifeng.com/blog/2011/03/url_hash.html get: 1.页面滚动到指定页面的指定位置 (eg: http://ww ...
- Office 2007在安装过程中出错-解决办法
1, 可能是因为c:\program files\common files\microsoft Shared\web server Extensions\40\bin目录下缺少Fp4autl.dll, ...
- dumpbin使用
声明一点:Win7系统,安装的是VS2010 dumpbin.exe位于C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin目录下. 初 ...
- python核心编程学习记录之基础知识
虽然对python的基础知识有所了解,但是为了更深入的学习,要对python的各种经典书籍进行学习 第一章介绍python的优缺点,略过 第二章介绍python起步,第三章介绍python基础,仅记录 ...
- 鸟哥的Linux私房菜之学习shell script
运行程序的时候一般都是创建一个子程序来执行,所以子程序中的变量什么的在当前的shell下没法使用,但是如果使用source来执行就可以在当前shell下执行程序 shift 1 去掉第一个参数,后面接 ...
- js操作记录
让checkbox全选 $("#checkall").click(function(){ $("input[name='checklist']").prop(& ...
- 【python cookbook】【数据结构与算法】15.根据字段将记录分组
问题:想根据字典或者对象实例的某个特定的字典(比如日期)来分组迭代数据 解决方案:itertools.groupby()函数在对数据进行分组时特别有用(前提是先以目标字典进行排序) rows = [ ...