-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0015_threeSum.cc
145 lines (136 loc) · 2.96 KB
/
Problem_0015_threeSum.cc
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <algorithm>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<vector<int>> threeSum1(vector<int> &nums)
{
std::sort(nums.begin(), nums.end());
vector<vector<int>> ans;
// 第一个数选了i位置的数
for (int i = 0; i < nums.size() - 2; i++)
{
// nums已经有序了,相同的数计算过一次可以不用计算
// nums[i - 1] != nums[i]说明当前的数与上一个数不一样
if (i == 0 || nums[i - 1] != nums[i])
{
vector<vector<int>> nexts = twoSum1(nums, i + 1, -nums[i]);
for (auto &cur : nexts)
{
cur.push_back(nums[i]);
ans.push_back(cur);
}
}
}
return ans;
}
// nums已经有序了
// nums[begin......]范围上,找到累加和为target的所有二元组
vector<vector<int>> twoSum1(vector<int> &nums, int begin, int target)
{
int L = begin;
int R = nums.size() - 1;
vector<vector<int>> ans;
while (L < R)
{
if (nums[L] + nums[R] > target)
{
R--;
}
else if (nums[L] + nums[R] < target)
{
L++;
}
else
{
if (L == begin || nums[L - 1] != nums[L])
{
vector<int> cur;
cur.push_back(nums[L]);
cur.push_back(nums[R]);
ans.emplace_back(cur);
}
L++;
}
}
return ans;
}
vector<vector<int>> threeSum2(vector<int> &nums)
{
std::sort(nums.begin(), nums.end());
int N = nums.size();
vector<vector<int>> ans;
for (int i = N - 1; i > 1; i--)
{
if (i == N - 1 || nums[i] != nums[i + 1])
{
vector<vector<int>> nexts = twoSum2(nums, i - 1, -nums[i]);
for (vector<int> cur : nexts)
{
cur.push_back(nums[i]);
ans.emplace_back(cur);
}
}
}
return ans;
}
vector<vector<int>> twoSum2(vector<int> &nums, int end, int target)
{
int L = 0;
int R = end;
vector<vector<int>> ans;
while (L < R)
{
if (nums[L] + nums[R] > target)
{
R--;
}
else if (nums[L] + nums[R] < target)
{
L++;
}
else
{
if (L == 0 || nums[L - 1] != nums[L])
{
vector<int> cur;
cur.push_back(nums[L]);
cur.push_back(nums[R]);
ans.emplace_back(cur);
}
L++;
}
}
return ans;
}
};
bool isSumEqualZero(vector<vector<int>> res)
{
for (int i = 0; i < res.size(); i++)
{
if (res[i][0] + res[i][1] + res[i][2] != 0)
{
return false;
}
}
return true;
}
void testThreeSum()
{
Solution s;
vector<int> n1 = {-1, 0, 1, 2, -1, -4};
vector<int> n2 = {0, 1, 1};
vector<int> n3 = {0, 0, 0};
EXPECT_TRUE(isSumEqualZero(s.threeSum1(n1)));
EXPECT_TRUE(isSumEqualZero(s.threeSum1(n2)));
EXPECT_TRUE(isSumEqualZero(s.threeSum1(n3)));
EXPECT_SUMMARY;
}
int main()
{
testThreeSum();
return 0;
}