-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_3_Sum.cpp
108 lines (97 loc) · 2.89 KB
/
15_3_Sum.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
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
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
/**
* @brief The Solution class
Given an array nums of n integers, are there elements a, b, c in nums such that
a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
*/
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size() < 3) return res;
sort(nums.begin(), nums.end());
size_t posstart=0, len=nums.size();
for(size_t i=0; i<len; i++)
{
if(nums.at(i) >= 0)
{
posstart = i;
break;
}
}
int temp=0, a=-1, b=-1, c=-1;
for(size_t i=0; i<=posstart; i++)
{
if(res.size()>0 && a==nums.at(i)) continue;
for(size_t j=i+1, k=len-1; j<k; )
{
if(res.size()>0 && a==nums.at(i) && b==nums.at(j))
{
j++;
continue;
}
temp = nums.at(i) + nums.at(j) + nums.at(k);
if(temp > 0)
k--;
else if(temp < 0)
j++;
else
{
a = nums.at(i);
b = nums.at(j);
c = nums.at(k);
res.push_back({a, b, c});
j++;
k--;
}
}
}
return res;
}
bool mycompare(vector<vector<int>> &v1, vector<vector<int>> &v2)
{
if(v1.size() != v2.size()) return false;
for(size_t i=0; i<v1.size(); i++)
{
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
if(v1.at(i).size() != v2.at(i).size()) return false;
for(size_t j=0; j<v1.at(i).size(); j++)
if(v1.at(i).at(j) != v2.at(i).at(j))
return false;
}
return true;
}
void test(vector<int> &nums, vector<vector<int>> &ans)
{
vector<vector<int>> res = threeSum(nums);
cout << "res" << (mycompare(ans, res) ? " == " : " != ") << "ans" << ":";
for(size_t i=0; i<nums.size(); i++)
cout << nums.at(i) << " ";
cout << endl;
}
};
int main()
{
vector<vector<int>> strs = {{-1, 0, 1, 2, -1, -4}, {0,0,0}, {-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6}};
vector<vector<vector<int>>> ans = {{{-1, 0, 1},{-1,-1,2}}, {{0,0,0}}, {{-4,-2,6},{-4,0,4},{-4,1,3},{-4,2,2},{-2,-2,4},{-2,0,2}}};
for(size_t i=0; i<ans.size(); i++)
{
Solution example;
example.test(strs.at(i), ans.at(i));
}
return 0;
}