170. Two Sum III - Data structure design
class TwoSum:
def __init__(self):
"""
Initialize your data structure here.
"""
self.table = defaultdict(int)
def add(self, number: int) -> None:
"""
Add the number to an internal data structure..
"""
self.table[number] += 1
def find(self, value: int) -> bool:
"""
Find if there exists any pair of numbers which sum is equal to the value.
"""
for key in self.table:
target = value - key
if target == key and self.table[key] >= 2:
return True
if target != key and target in self.table:
return True
return False雙指針
Last updated