-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_sum.cpp
More file actions
57 lines (51 loc) 路 1.55 KB
/
Copy path3_sum.cpp
File metadata and controls
57 lines (51 loc) 路 1.55 KB
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
// INCOMPLETE!!!
class Solution
{
public:
vector<vector<int>> twoSum(vector<int> &nums, int idx, vector<bool> &doneList)
{
vector<vector<int>> result;
unordered_map<int, int> h_map;
int j = 0;
for (int i = 0; i < nums.size(); i++)
{
if (i == idx || doneList[i])
continue;
j = -nums[idx] - nums[i];
if (h_map.count(j) > 0)
{
result.push_back({nums[i], nums[h_map[j]]});
}
else
{
h_map[nums[i]] = i;
}
}
return result;
}
vector<vector<int>> threeSum(vector<int> &nums)
{
vector<vector<int>> soln;
if (nums.size() < 3)
return soln;
unordered_map<long, bool> item_exists_map;
vector<bool> doneList(nums.size(), false);
for (int i = 0; i < nums.size(); i++)
{
doneList[i] = true;
vector<vector<int>> res = twoSum(nums, i, doneList);
for (int j = 0; j < res.size(); j++)
{
res[j].push_back(nums[i]);
long hash = res[j][0] ^ res[j][1] ^ res[j][2];
// cout << nums[i] << " -> " << res[j][0] << "," << res[j][1] << "," << res[j][2] << " | " << hash << endl;
if (item_exists_map.count(hash) > 0)
continue;
item_exists_map[hash] = true;
soln.push_back(res[j]);
}
// cout << "---\n";
}
return soln;
}
};