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.
Note:
- 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.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
分析:
先吐槽一下这个名字起得= = 是只有我自己邪恶了吗?
经过测试发现N^2的解法和N^2LogN的解法时间差不了太多。
具体分析可以看https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.02.md
直接上代码
解法一(N^2):
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > ans;
int N = num.size();
if(N < 3)
return ans;
sort(num.begin(),num.end());
vector<int> tmp(num);
int x = 2147483647;
for(int i = 0; i < N - 2; i++){
if(num[i] + num[i + 1] + num[i + 2] > 0)
break;
if(num[i] == x)
continue;
else
x = num[i];
tmp.erase(tmp.begin() + i);
int s = i , t = N - 2;
while(s < t){
if(tmp[s] + tmp[t] == 0 - x){
if(s != t){
vector<int> v{x,tmp[s],tmp[t]};
sort(v.begin(),v.end());
ans.push_back(v);
while(s < t && tmp[s+1] == tmp[s]) s++;
s++;
while(s < t && tmp[t-1] == tmp[t]) t--;
t--;
}
else{
if(s < N - 2 && tmp[s + 1] == tmp[s])
s++;
else if(t > 0 && tmp[t - 1] == tmp[t])
t--;
else
s++, t--;
}
}else if(tmp[s] > 0 - x - tmp[t]){
t--;
}else{
s++;
}
}
tmp.insert(tmp.begin() + i, x);
}
return ans;
}
};解法二(N^2logN)
<span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);">class Solution {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> public:</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> vector<vector<int>> </span><span style="color:#000000;font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"><span style="background-color: rgb(255, 255, 0);">threeSum</span></span><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);">(vector<int> &num) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> vector<vector<int>> result;</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> sort(num.begin(), num.end());</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> if (num.size() < 3) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> return result;</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> for (auto p = num.begin(); distance(p, num.end()) >= 2; p = upper_bound(p, num.end(), *p)) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> if (*p + *(p + 1) + *(p + 2) > 0) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> return result;</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> for (auto q = p + 1; distance(q, num.end()) >= 1; q = upper_bound(q, num.end(), *q)) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> if (binary_search(q + 1, num.end(), -(*p + *q))) {</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> vector<int> tmp{*p, *q, -(*p + *q)};</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> result.push_back(tmp);</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> return result;</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);"> }</span><br style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);" /><span style="font-family: MicrosoftSansSerif, 'MS Sans Serif', sans-serif; background-color: rgb(255, 237, 196);">};</span>

1713

被折叠的 条评论
为什么被折叠?



