forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
134 lines (119 loc) · 4.37 KB
/
3Sum.cpp
File metadata and controls
134 lines (119 loc) · 4.37 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
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
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: June 18, 2012
Problem: 3 Sum
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
The solution set must not contain duplicate triplets.
Solution:
This problem is the extension of the problem below:
Given a set S of n integers, find all pairs of integers of a and b in S such that a + b = k?
The above problem can be solved in O(n) time, assuming that the set S is already sorted. By
using two index first and last, each pointing to the first and last element, we look at the
element pointed by first, which we call A. We know that we need to find B = k – A, the
complement of A. If the element pointed by last is less than B, we know that the choice is
to increment pointer first by one step. Similarly, if the element pointed by last is greater
than B, we decrement pointer last by one step. We are progressively refining the sum step by
step. Since each step we move a pointer one step, there are at most n steps, which gives the
complexity of O(n).
By incorporating the solution above, we can solve the 3sum problem in O(n^2) time, which is
a straight forward extension.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
using namespace std;
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
set<vector<int> > solset;
vector<int> triple(3);
int i, j, k;
int n = num.size();
int sum;
sort(num.begin(), num.end());
for (i = 0; i < n; i++) {
if (i == 0) {
sum = -num[i];
}
else {
if (sum == -num[i]) {
continue;
}
sum = -num[i];
}
for (j = 0, k = n - 1; j < k; ) {
if (num[j] + num[k] < sum || j == i) {
j++;
}
else if (num[j] + num[k] > sum || k == i) {
k--;
}
else {
triple[0] = -sum;
triple[1] = num[j];
triple[2] = num[k];
sort(triple.begin(), triple.end());
solset.insert(triple);
j++;
k--;
}
}
}
vector<vector<int> > sol(solset.begin(), solset.end());
return sol;
}
};
// an O(n^2) time, O(1) space solution by Ke Hu (mrhuke@gmail.com), Apr. 2013
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int> > res;
for ( int i = 0; i < num.size(); ++i){
if (i != 0 && num[i] == num[i-1]) continue;
for ( int j = i +1, k = num.size()-1; j < k; ){
if (num[i] + num[j] + num[k] == 0){
int tmp[] = {num[i], num[j], num[k]};
vector<int> triple = vector<int>(tmp, tmp+3);
res.push_back(triple);
++j; --k;
while(j < num.size()-1 && num[j] == num[j-1]) ++j;
while(k > 0 && num[k] == num[k+1]) --k;
}
else if (num[i] + num[j] + num[k] < 0)
++j;
else
--k;
}
}
return res;
}
};
// a less efficient O(n^2) time, O(n) space solution by Ke Hu (mrhuke@gmail.com), Apr. 2013
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
set<vector<int> > res;
for ( int i = 0; i < num.size(); ++i){
int sum2 = -num[i];
set<int> h;
for ( int j = i+1; j < num.size(); ++j){
if (!h.empty() && h.find(num[j]) != h.end()){
int tmp[] = {num[i], num[j], -num[i]-num[j]};
sort(tmp, tmp+3);
vector<int> triple(tmp, tmp+3);
res.insert(triple);
}
h.insert(sum2 - num[j]);
}
}
return vector<vector<int> >(res.begin(), res.end());
}
};