LeetCode - 3Sum 题解

Given an array S of n integers, are there elements abc 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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值