-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRuslan_Arutyunyan.cpp
74 lines (69 loc) · 2.49 KB
/
Ruslan_Arutyunyan.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
public:
vector<vector<int>> threeSum(const vector<int>& nums) {
vector<vector<int>> result;
unordered_map<int, int> negative;
unordered_map<int, int> nonNegative;
// Fill negative and non-negative hash maps
fillMaps(negative, nonNegative, nums);
auto nonNegEnd(nonNegative.end());
// Searching the sum of two each element of negative map in non-negative
findAndPushTriplet(negative.begin(), negative.end(), nonNegative, result);
// Searching the sum of two each element of non-negative map in negative
findAndPushTriplet(nonNegative.begin(), nonNegEnd, negative, result);
// Corner case for three zeros
auto zeroIt(nonNegative.find(0));
if (zeroIt != nonNegEnd && zeroIt->second > 2)
{
result.push_back({ 0, 0, 0 });
}
return result;
}
private:
void fillMaps(unordered_map<int, int>& negative, unordered_map<int, int>& nonNegative, const vector<int>& nums) const;
template <typename Begin, typename End>
void findAndPushTriplet(Begin&& begin, End&& end, const unordered_map<int, int>& oposite, vector<vector<int>>& result);
};
void Solution::fillMaps(unordered_map<int, int>& negative, unordered_map<int, int>& nonNegative, const vector<int>& nums) const
{
for (auto& a : nums)
{
if (a < 0)
{
++negative[a];
}
else
{
++nonNegative[a];
}
}
}
template <typename Begin, typename End>
void Solution::findAndPushTriplet(Begin&& begin, End&& end, const unordered_map<int, int>& oposite, vector<vector<int>>& result)
{
auto opositeEnd(oposite.end());
for (auto it(begin); it != end; ++it)
{
// Checking number count in original input for solutions like [1,1,-2]
if (it->second > 1)
{
auto opositeIt(oposite.find(-(it->first * 2)));
if (opositeIt != opositeEnd)
{
result.emplace_back(std::initializer_list<int>{it->first, it->first, opositeIt->first });
}
}
// calculate sum of two for each element in map.
auto it2(it);
++it2;
for (; it2 != end; ++it2)
{
auto sum(it2->first + it->first);
auto opositeIt(oposite.find(-sum));
if (opositeIt != opositeEnd)
{
result.emplace_back(std::initializer_list<int>{ it->first, it2->first, opositeIt->first });
}
}
}
}