Two Sum III - Data Structure Design

Problem: Two Sum III - Data Structure Design

Similar to classic Two Sum problem.

Code in Python:

class TwoSum(object):

    def __init__(self):
        """
        initialize your data structure here
        """
        self.dict = {}


    def add(self, number):
        """
        Add the number to an internal data structure.
        :rtype: nothing
        """
        if number not in self.dict: self.dict[number] = 1
        else:
            self.dict[number] += 1


    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 number in self.dict:
            if value-number in self.dict:
                if value-number == number and self.dict[number] >= 2: return True
                elif value-number != number: return True
        return False

results matching ""

    No results matching ""