|
| 1 | +<h2><a href="https://leetcode.com/problems/two-sum-iii-data-structure-design">170. Two Sum III - Data structure design</a></h2><h3>Easy</h3><hr><p>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.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>TwoSum</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>TwoSum()</code> Initializes the <code>TwoSum</code> object, with an empty array initially.</li> |
| 7 | + <li><code>void add(int number)</code> Adds <code>number</code> to the data structure.</li> |
| 8 | + <li><code>boolean find(int value)</code> Returns <code>true</code> if there exists any pair of numbers whose sum is equal to <code>value</code>, otherwise, it returns <code>false</code>.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<strong>Input</strong> |
| 16 | +["TwoSum", "add", "add", "add", "find", "find"] |
| 17 | +[[], [1], [3], [5], [4], [7]] |
| 18 | +<strong>Output</strong> |
| 19 | +[null, null, null, null, true, false] |
| 20 | + |
| 21 | +<strong>Explanation</strong> |
| 22 | +TwoSum twoSum = new TwoSum(); |
| 23 | +twoSum.add(1); // [] --> [1] |
| 24 | +twoSum.add(3); // [1] --> [1,3] |
| 25 | +twoSum.add(5); // [1,3] --> [1,3,5] |
| 26 | +twoSum.find(4); // 1 + 3 = 4, return true |
| 27 | +twoSum.find(7); // No two integers sum up to 7, return false |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>-10<sup>5</sup> <= number <= 10<sup>5</sup></code></li> |
| 35 | + <li><code>-2<sup>31</sup> <= value <= 2<sup>31</sup> - 1</code></li> |
| 36 | + <li>At most <code>10<sup>4</sup></code> calls will be made to <code>add</code> and <code>find</code>.</li> |
| 37 | +</ul> |
0 commit comments