-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-3Sum.cpp
72 lines (65 loc) · 1.88 KB
/
15-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*=============================================================================
# FileName: 15-3Sum.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-04-05 05:18:02
# Version: 0.0.1
# LastChange: 2015-04-05 05:18:02
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
//73ms
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > result;
vector<int> temp(3, 0);
if (num.size() < 3) {
return result;
}
sort(num.begin(), num.end());
for (int i = 0; i < num.size() - 2; i ++) {
if (result.size() > 0 && result.back()[0] == num[i]) {
continue;
}
temp[0] = num[i];
int l = i + 1, r = num.size() - 1;
while (l < r) {
if (num[l] + num[r] == -num[i]) {
temp[1] = num[l];
temp[2] = num[r];
result.push_back(temp);
l ++;
r --;
while (l < r && num[l] == num[l - 1]) {
l ++;
}
while (l < r && num[r] == num[r + 1]) {
r --;
}
} else if (num[l] + num[r] > -num[i]) {
r --;
} else {
l ++;
}
}
}
return result;
}
};
int main() {
int num[] = {0, 0, 0};
vector<int> vec;
for (int i = 0; i < sizeof(num) / sizeof(int); i ++) {
vec.push_back(num[i]);
}
Solution solution;
solution.threeSum(vec);
return 0;
}