def find(self, value):
"""
Find if there exists any pair of numbers which sum is equal to the value.
:type value: int
:rtype: bool
"""
dic = {}
for i in range(len(self.list)):
if value-self.list[i] in dic:
return True
else:
dic[self.list[i]]=i
return False
1 设计twosum用hashtable的时候,dic[self.list[i]]=i,这里需要把value当做key,i当做value,这是因为用target-value的时候,是看这个value是不是在dic里。if value-self.list[i] in dic:这句code是看值是否在dic里,所以需要做上述调换