-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq_015_3sum.cpp
More file actions
57 lines (57 loc) · 1.87 KB
/
q_015_3sum.cpp
File metadata and controls
57 lines (57 loc) · 1.87 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
class Solution {
public:
vector<int> sort3(int a, int b, int c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
vector<int> r {a, b, c};
return r;
}
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> r;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
int third = - nums[i] - nums[j];
if (find(nums.begin() + j + 1, nums.end(), third) != nums.end()) {
vector<int> n3 {nums[i], nums[j], third};
if (find(r.begin(), r.end(), n3) == r.end()){
r.push_back(n3);
}
}
}
}
return r;
/* =================================
* Solution 1: brutal force exceed time limit
vector<vector<int>> r;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
for (int k = j + 1; k < nums.size(); ++k) {
if (nums[i] + nums[j] + nums[k] == 0) {
// for (auto n : nums) cout << n << ", ";
auto n3 = sort3(nums[k], nums[j], nums[i]);
if (find(r.begin(), r.end(), n3) == r.end()) {
r.push_back(sort3(nums[k], nums[j], nums[i]));
}
// cout << endl;
// for (auto n : nums) cout << n << ", ";
// cout << endl;
// for (auto n : r[0]) cout << n << ", ";
}
}
}
}
return r; */
}
};