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

查找表_leetcode219

时间:2019-03-17 15:50:42      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:思路   contains   保存   return   add   solution   set   contain   on()   

# 解题思路:用查找表(集合),保存其K个值的状态  20190302 找工作期间

class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if len(nums) <=1 :
return False

if k <= 0:
return False


record = set()
for i in range(len(nums)):

if nums[i] in record:
return True
else:
record.add(nums[i])

if len(record) == k+1:
record.remove(nums[i-k])

return False


# n = [1,0,1,1]
# k = 1

n = [1,2,3,1,2,3]
k = 2
s = Solution()
print s.containsNearbyDuplicate(n,k)

查找表_leetcode219

标签:思路   contains   保存   return   add   solution   set   contain   on()   

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

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