0170. Two Sum III - Data structure design
https://leetcode.com/problems/two-sum-iii-data-structure-design
Description
Design a data structure that accepts a stream of integers and checks if it has a pair of integers that sum up to a particular value.
Implement the TwoSum
class:
TwoSum()
Initializes theTwoSum
object, with an empty array initially.void add(int number)
Addsnumber
to the data structure.boolean find(int value)
Returnstrue
if there exists any pair of numbers whose sum is equal tovalue
, otherwise, it returnsfalse
.
Example 1:
Constraints:
-105 <= number <= 105
-231 <= value <= 231 - 1
At most
104
calls will be made toadd
andfind
.
ac
worst case is O(n) in finding as well...
Last updated