Solution: Two Sum III - Data structure design
Understand how to implement the TwoSum class that supports adding numbers and checking if any two numbers sum to a specific target. This lesson helps you learn to design and utilize a hash map to track number frequencies, enabling efficient pair-sum queries with time complexity O(N). You will also explore constraints and method implementations that solve the problem effectively.
We'll cover the following...
Statement
Design a data structure that takes in a stream of numbers and can check if any two numbers add up to a specific value.
Implement the TwoSum class with the following constructor and methods:
Constructor: Sets up the
TwoSumobject with an empty list at the start.void add(int number): Adds a new number to the list.
boolean find(int value): Returns TRUE if any two numbers in the list add up to the given value. If not, it returns FALSE.
Constraints: ...