leetcode之Intersection of Two Arrays II
2016-06-09 02:36
405 查看
这题跟上一题http://blog.csdn.net/howtogetout/article/details/51619443不一样的地方在于,这题要求的是尽可能大的公共部分,包含了重复数字。
老样子,使用了Counter函数来计数,去重。
老样子,使用了Counter函数来计数,去重。
from collections import Counter class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ if nums1 == [] or nums2 == []: return [] c1 = Counter(nums1) c2 = Counter(nums2) length1 = len(c1.keys()) length2 = len(c2.keys()) result = [] if length1 <= length2: for i in c1.keys(): if i in c2.keys(): if c1[i] >= c2[i]: result = result + [i] * c2[i] else: result = result + [i] * c1[i] else: for i in c2.keys(): if i in c1.keys(): if c1[i] >= c2[i]: result = result + [i] * c2[i] else: result = result + [i] * c1[i] return result
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法