-
Notifications
You must be signed in to change notification settings - Fork 0
/
3sum.cpp
53 lines (41 loc) · 1.29 KB
/
3sum.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
/*
* 3sum: http://www.leetcode.com/2010/04/finding-all-unique-triplets-that-sums.html
* Song Qiang <qiang.song@usc.edu> 2012-10-04
*
*/
// time complexity: O(n^2)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
if (num.size() < 3) return vector<vector<int> >();
std::sort(num.begin(), num.end());
vector<vector<int> > r;
vector<int> threenum(3);
int i = 0;
while (i < num.size() && num[i] + num.back() + num[num.size() - 2] < 0) ++i;
while (i < num.size() - 2 && num[i] <= 0)
{
const int s = - num[i];
threenum[0] = num[i];
int h = i + 1, t = num.size() - 1;
while (h < t)
{
if (num[h] + num[t] == s)
{
threenum[1] = num[h];
threenum[2] = num[t];
if (r.empty() || r.back() != threenum) r.push_back(threenum);
++h;
--t;
}
else if (num[h] + num[t] < s)
++h;
else
--t;
}
++i;
while (i < num.size() && num[i] == num[i - 1]) ++i;
}
return r;
}
};