You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classTwoSum:
""" @param number: An integer @return: nothing """def__init__(self):
fromcollectionsimportdefaultdictself.cache=defaultdict(int)
defadd(self, number):
# write your code here## add(5) >>> self.cache {5:0} self.cache[number] +=1""" @param value: An integer @return: Find if there exists any pair of numbers which sum is equal to the value. """deffind(self, value):
# write your code here## 最难就是两个一样forkinself.cache.keys():
ifk==value-k:
ifself.cache.get(value-k) >1:
returnTrueelse :
ifself.cache.get(value-k):
returnTruereturnFalse# 插入0(1),查找O(N)