[LeetCode][Python]Intersection of Two Arrays
2016-05-19 19:53
661 查看
Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example:
Given nums1 =
[1, 2, 2, 1], nums2 =
[2, 2], return
[2].
Note:
Each element in the result must be unique.
The result can be in any order.
https://leetcode.com/problems/intersection-of-two-arrays/
求两个数组的交集。
先遍历nums1,第一个哈希表记录所有nums1中出现过的元素。
再遍历nums2,第二个哈希表记录已经在结果中的元素。
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ res = []; dictionary = {}; addedNum = {} for num in nums1: dictionary[num] = True; for num in nums2: if dictionary.has_key(num) and not addedNum.has_key(num): res.append(num); addedNum[num] = True; return res;
相关文章推荐
- 深入super,看Python如何解决钻石继承难题
- python部落之python刷题宝之基础知识
- Python测试框架doctest
- python数据类型和变量
- python的I/O
- 第一个python小程序
- Python函数常用方法
- 安装python
- Python Tensorflow下的Word2Vec代码解释
- python 实现终端中的进度条
- Python 之 sklearn 交叉验证 数据拆分
- 基于Flask的博客系统搭建
- Python学习笔记—PyQuery库的使用总结
- Python 装饰器
- 量化投资策略:常见的几种Python回测框架(库)
- python通过colorama模块在控制台输出彩色文字的方法
- Python实现C代码统计工具(一)
- python数据分析4:双色球 两个红和蓝球哪组合比例高
- python字典操作
- python:在Django项目中使用Celery实现定时任务--简单版