-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path170*.cpp
57 lines (50 loc) · 1.31 KB
/
170*.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 170. Two Sum III - Data structure design - https://leetcode.com/problems/two-sum-iii-data-structure-design
#include "bits/stdc++.h"
using namespace std;
//Design and implement a TwoSum class.
//It should support the following operations: add and find.
//
//add - Add the number to an internal data structure.
//find - Find if there exists any pair of numbers which
//sum is equal to the value.
//
//For example,
//add(1);
//add(3);
//add(5);
//find(4) -> true
//find(7) -> false
class TwoSum {
// number - count
unordered_map<int, int> countTable;
public:
void add(int number) {
countTable[number]++;
}
bool find(int value) {
for (const auto &nc : countTable)
if (countTable.count(value - nc.first) && (nc.first != value - nc.first || nc.second > 1))
return true;
return false;
}
};
// class TwoSum {
// unordered_multiset<int> nums;
// public:
// void add(int number) {
// nums.insert(number);
// }
// bool find(int value) {
// for (int i : nums) {
// int count = i == value - i ? 1 : 0;
// if (nums.count(value - i) > count) {
// return true;
// }
// }
// return false;
// }
// };
int main() {
ios::sync_with_stdio(false);
return 0;
}