Sum of Two Numbers - Online Version
Implement a data structure with the following methods:
add(int val)
adds the valueval
to the data structurefind(int val)
returns whether there are two elements whose sum equals toval
Constraints
n ≤ 10,000
wheren
is the number of timesadd
will be calledm ≤ 1,000
wherem
is the number of timesfind
will 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
TwoSum
first - Then we add the number
5
to 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
6
to the data structure - We check if there’s two numbers whose sum is
11
, which there is since5 + 6 = 11
.
Leave a comment