forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.cpp
81 lines (72 loc) · 2.65 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
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
/*
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;
}
};