-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy path3sum.cpp
More file actions
61 lines (57 loc) · 1.25 KB
/
3sum.cpp
File metadata and controls
61 lines (57 loc) · 1.25 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
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> threeSum(vector<int> &nums)
{
vector<vector<int>> sol;
if (nums.size() < 3)
{
return sol;
}
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++)
{
cout << "A\n";
int l = i + 1;
int r = nums.size() - 1;
while (l < r)
{
cout << "B" << endl;
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0)
{
cout << "C\n";
vector<int> t = {nums[i], nums[l], nums[r]};
cout << "D\n";
sol.push_back(t);
l++;
}
else if (sum > 0)
{
cout << "E\n";
r--;
}
else
{
cout << "F\n";
l++;
}
}
}
sol.erase(unique(sol.begin(), sol.end()), sol.end());
return sol;
}
int main()
{
vector<int> nums = {-1, 0, 1, 2, -1, -4};
vector<vector<int>> sol = threeSum(nums);
for (vector<int> i : sol)
{
for (int j : i)
{
cout << j << " ";
}
cout << endl;
}
cout << "hello there";
return 0;
}