https://leetcode.com/problems/contains-duplicate-iii/
思路1 sorting
参考http://yucoding.blogspot.hk/2015/10/leetcode-question-contains-duplicate-iii.html
思路就是带着index一起,先对value进行排序,然后从左到右用p scan,然后对i = p + 1, 再scan后面的数,如果val的差已经大于t那么就不用继续往后scan了。这里复杂度应该还是O(n^2)
class Solution(object):
def search(self, l, t, k):
po = 0
while po < len(l):
i = po + 1
while i < len(l):
if abs(l[i][0] - l[po][0]) <= t and abs(l[i][1] - l[po][1]) <= k:
return True
else:
if abs(l[i][0] - l[po][0]) > t:
break
else:
i +=1
po += 1
return False
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
pp = sorted(zip(nums, range(len(nums))), key= lambda x:x[0])
return self.search(pp,t ,k )
思路2 sliding windows
参考http://bookshadow.com/weblog/2015/06/03/leetcode-contains-duplicate-iii/
这个思路还没理解。再看看