码迷,mamicode.com
首页 > 其他好文 > 详细

查找表_leetcode350

时间:2019-03-17 15:25:58      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:工作   leetcode   pen   object   weight   解题思路   list   self   style   

# 解题思路:字典 20190302 找工作期间

class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""

dic = dict()
res = []

for i in range(len(nums1)):
if nums1[i] not in dic:
dic[nums1[i]] = 1
else:
dic[nums1[i]] += 1

for j in range(len(nums2)):
if nums2[j] in dic:
if dic[nums2[j]] > 0 :
res.append(nums2[j])
dic[nums2[j]] -= 1
return res


s = Solution()

n1 = [4,9,5]
n2 = [9,4,9,8,4]

print s.intersect(n1,n2)

查找表_leetcode350

标签:工作   leetcode   pen   object   weight   解题思路   list   self   style   

原文地址:https://www.cnblogs.com/lux-ace/p/10546955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!