Sum of Two Numbers - Online Version
Implement a data structure with the following methods:
add(int val)adds the valuevalto the data structurefind(int val)returns whether there are two elements whose sum equals toval
Constraints
n ≤ 10,000wherenis the number of timesaddwill be calledm ≤ 1,000wheremis the number of timesfindwill be called
https://binarysearch.com/problems/Sum-of-Two-Numbers-Online-Version
Examples
Example 1
Input
- methods =
['constructor', 'add', 'find', 'add', 'find'] - arguments =
[[], [5], [10], [6], [11]]
Output
- answer =
[None, None, False, None, True]
Explanation
- We create a
TwoSumfirst - Then we add the number
5to the data structure - We check if there’s two numbers whose sum is
10. There isn’t, so we returnfalse - Then we add the number
6to the data structure - We check if there’s two numbers whose sum is
11, which there is since5 + 6 = 11.
Leave a comment