File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 163163| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | [](src/TwoSumIIInputArrayIsSorted.java) [](python/two_sum_ii.py) | |
164164| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [](src/ExcelSheetColumnTitle.java) [](python/excel_sheet_column_title.py) | |
165165| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | [](src/MajorityElement.java) [](python/majority_element.py) | |
166- | 170 | 🔒 [Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | | |
166+ | 170 | 🔒 [Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | [](src/TwoSumIIIDataStructureDesign.java) | |
167167| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number) | [](src/ExcelSheetColumnNumber.java) [](python/excel_sheet_column_number.py) | |
168168| 172 | [Factoring Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes) | [](src/FactorialTrailingZeros.java) [](python/factorial_trailing_zeroes.py) | |
169169| 173 | [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator) | [](src/BinarySearchTreeIterator.java) | |
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class TwoSumIIIDataStructureDesign {
5+ static class TwoSum {
6+ private final Map <Integer , Integer > map = new HashMap <>();
7+
8+ // T: O(1)
9+ public void add (int number ) {
10+ map .put (number , map .getOrDefault (number , 0 ) + 1 );
11+ }
12+
13+ // N: number of unique elements we have seen so far
14+ // T: O(N)
15+ public boolean find (int sum ) {
16+ for (int current : map .keySet ()) {
17+ final int required = sum - current ;
18+ if (required == current ) {
19+ if (map .get (required ) >= 2 ) {
20+ return true ;
21+ } else {
22+ continue ;
23+ }
24+ }
25+
26+ if (map .containsKey (required )) {
27+ return true ;
28+ }
29+ }
30+
31+ return false ;
32+ }
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments