首页 > 其他 > 详细

查找表_leetcode220

时间:2019-03-17 14:52:16      阅读:132      评论:0      收藏:0      [点我收藏+]
# 解题思路:用查找表(集合),保存其K个值的状态  遍历查找表时加上了限制条件T 20190302 找工作期间

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

if k<=0 or t < 0:
return False

record = set()
l = len(nums)

for i in range(l):
if t == 0:
if nums[i] in record:
return True
else:
for j in record:
if abs(nums[i]-j) <= t:
return True



record.add(nums[i])

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


return False


class Solution2(object):
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
lenth = len(nums)
a = set()
for i in range(lenth):
if t==0:
if nums[i] in a:
return True
else:
for atem in a:
if abs(nums[i]-atem)<=t:
return True
a.add(nums[i])
if len(a) == k+1:
a.remove(nums[i-k])
return False

查找表_leetcode220

原文:https://www.cnblogs.com/lux-ace/p/10546929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!