class TwoSum(object):
def __init__(self):
"""
initialize your data structure here
"""
self.internal = []
self.dic = {}
def add(self, number):
"""
Add the number to an internal data structure.
:rtype: nothing
"""
self.internal.append(number)
if number in self.dic:
# more than once
self.dic[number] = True
return
# once
self.dic[number] = False
def find(self, value):
"""
Find if there exists any pair of numbers which sum is equal to the value.
:type value: int
:rtype: bool
"""
for v in self.internal:
if value - v in self.dic:
if v << 1 == value and not self.dic[v]:
continue
return True
return False
# Your TwoSum object will be instantiated and called as such:
# twoSum = TwoSum()
# twoSum.add(number)
# twoSum.find(value)
Download Two Sum III-Data structure design.pyLeetcode 170 Two Sum III-Data structure design problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.