// Add-Heavy
class TwoSum {
public:
/** Initialize your data structure here. */
TwoSum() {
}
/** Add the number to an internal data structure.. */
void add(int number) {
++mp[number];
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
bool find(int value) {
for (auto a : mp) {
int t = value - a.first;
if ((t != a.first && mp.count(t)) || (t == a.first && mp[t] >= 2))
return true;
}
return false;
}
private:
unordered_map<int, int> mp;
};
// Find-Heavy
class TwoSum {
public:
/** Initialize your data structure here. */
TwoSum() {
}
/** Add the number to an internal data structure.. */
void add(int number) {
if (nums.count(number)) {
sums.insert(number * 2);
} else {
for (auto it = nums.begin(); it != nums.end(); ++it) {
sums.insert(*it + number);
}
nums.insert(number);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
bool find(int value) {
return sums.count(value);
}
private:
unordered_set<int> nums, sums;
};