查找表_leetcode350

博客展示了LeetCode350题的解题思路,使用字典实现查找表。定义了Solution类的intersect方法,通过遍历两个列表,利用字典记录元素出现次数,找出交集元素。最后给出示例代码并打印结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 解题思路:字典 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)

转载于:https://www.cnblogs.com/lux-ace/p/10546955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值