Leetcode每日一题:15.3sum(三数之和)

思路:这道题与双指针法类似,使用三指针法 头尾各一个 中间一个来回扫,重点是如何剪枝,想了会我也只能剪出下面这样子了,但还是只超5%;
在这里插入图片描述
评论区笑傻:在这里插入图片描述

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

vector<vector<int>> threeSum(vector<int> &nums)
{
    vector<vector<int>> res;
    if (nums.size() < 3)
        return res;

    int length = nums.size();
    //三指针法 头指针一个指0  尾指针两个指length-2 和length-1
    int oneP = 0;
    int twoP;
    int threeP = length - 1;

    set<vector<int>> temp;          //先放 temp 集合用于去重
    sort(nums.begin(), nums.end()); //从小到大排序 NlgN

    while (oneP < threeP - 1)
    {
        twoP = oneP + 1;
        for (; twoP < threeP; twoP++)
        {
            int sum = nums[oneP] + nums[twoP] + nums[threeP];
            if (sum == 0) //如果和为零  插入 , 且此时右边两个指针都不用往左移,因为左移之后只可能小于0 or 重复
            {
                vector<int> vec;
                vec.push_back(nums[oneP]);
                vec.push_back(nums[twoP]);
                vec.push_back(nums[threeP]);
                temp.insert(vec);
                continue;
            }
            else if (sum > 0)
            {
                twoP--;//保持twoP不动
                threeP--;
            }
            else //sum<0
            {
                continue;
            }
            
        }
        threeP=length-1;//第三指针归位
        oneP++;
    }
    //把temp集合里的vector都放res中
    for (auto v : temp) //集合中的每个vector
    {
        res.push_back(v);
    }
    return res;
}

int main()
{
    vector<int> test;
    test.push_back(-1);
    test.push_back(0);
    test.push_back(1);
    test.push_back(2);
    test.push_back(-1);
    test.push_back(-4);
    vector<vector<int>> result;
    result = threeSum(test);
    for (auto a : result)
    {
        for (auto b : a)
        {
            cout << b << " ";
        }
        cout << endl;
    }
    test.clear();
    test.push_back(-2);
    test.push_back(0);
    test.push_back(1);
    test.push_back(1);
    test.push_back(2);
    result = threeSum(test);
    for (auto a : result)
    {
        for (auto b : a)
        {
            cout << b << " ";
        }
        cout << endl;
    }
    return 0;
}

如有建议,感谢指明!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值