-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015_3Sum.cpp
More file actions
100 lines (99 loc) · 2.73 KB
/
015_3Sum.cpp
File metadata and controls
100 lines (99 loc) · 2.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<vector>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iterator>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
if(nums.size() < 3) return {};
if(count(nums.begin(), nums.end(), 0) == nums.size())
return {{0,0,0}};
unordered_map<int,int> values;
for(auto it : nums)
{
values[it]++;
}
set<vector<int>> result;
for(size_t i = 0; i < nums.size()-1; i++)
{
for(size_t j = i+1; j < nums.size();j++)
{
int a = nums[i], b = nums[j], c = 0- a-b;
if(values.find(c) != values.end())
{
if(c==a || c==b)
{
if( a == b)
{
//a==b==c,=0
if(values.at(c) < 3)
continue;
}
else
{
//a, b==c or b, a==c
if(values.at(c) < 2)
continue;
}
}
int temp[]={a,b,c};
sort(temp, temp+3);
result.insert({temp, temp+3});
}
}
}
return {result.begin(), result.end()};
}
vector<vector<int>> threeSum1(vector<int>& nums)
{
set<vector<int>> result;
if(nums.size() < 3) return {};
if(count(nums.begin(), nums.end(), 0) == nums.size())
return {{0,0,0}};
sort(nums.begin(), nums.end());
for(auto it = nums.begin(); it != (nums.end()-2); it++)
{
int a = *it;
auto start = it+1;
auto end = nums.end()-1;
while(start < end)
{
int b = *start;
int c= *end;
if(a+b+c == 0)
{
result.insert({a,b,c});
start++;
end--;
}
else if(a+b+c > 0)
end--;
else if(a+b+c < 0)
start++;
}
}
return {result.begin(), result.end()};
}
};
int main(int argc, char const *argv[])
{
Solution sol;
vector<int> nums ={-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0};
//{-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6};
//{-1, 0, 1, 2, -1, -4};
auto ret = sol.threeSum(nums);
for(auto e: ret)
{
for(auto ee: e)
cout<<ee<<"\t";
cout<<endl;
}
return 0;
}