LeetCode-三数之和(排序,固定一个数,然后双指针寻找另外两个数)

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
   [-1, 0, 1],
   [-1, -1, 2]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum

分析:

暴力法的复杂度是O(N^3),肯定会超时

先将数组排序,假设k1<=k2<=k3,先固定k1,然后通过双指针法在k1的后面寻找k2和k3

可以利用的性质:

1.如果k1大于0,则三数之和肯定无法等于0,可以跳过该k1

2.如果v[i]==v[i-1],那么i-1可以跳过,因为这样必然会导致重复结果

可以采用set去除重复结果

时间复杂度:O(N^2)

排序的复杂度是O(N*log N),但是后续固定看+双指针的复杂度是O(N^2),二者取大的,所以时间复杂度是O(N^2)

空间复杂度:快排需要,最好情况:O(log N),最坏情况:O(N)

code:

class Solution {
public:
vector<vector<int> > threeSum(vector<int>& a)
{
    vector<vector<int> > vv;
    vector<int> v;
    set<vector<int> > s;
    set<vector<int> >::iterator it;
    int n=a.size();
    if(n<3)
        return vv;
    sort(a.begin(),a.end());
    for(int k=0;k<n-2;k++)
    {
        if(a[k]>0)
            continue;
        if(k>0&&a[k-1]==a[k])
            continue;
        int l=k+1;
        int h=n-1;
        //cout<<"k="<<k<<"l="<<l<<" h="<<h<<endl;
        s.clear();
        while(l<h)
        {
            int ans=a[k]+a[l]+a[h];
            //cout<<"l="<<l<<" h="<<h<<"ans="<<ans<<endl;
            if(ans==0)
            {
                v.clear();
                v.push_back(a[k]);
                v.push_back(a[l]);
                v.push_back(a[h]);
                s.insert(v);
                l++;
                h--;
            }else if(ans<0)
            {
                l++;
            }else if(ans>0)
            {
                h--;
            }
        }
        if(s.size()!=0)
        {
            for(it=s.begin();it!=s.end();it++)
            {
                vv.push_back(*it);
            }
        }
    }
    return vv;
}
};

原文地址:https://www.cnblogs.com/yinbiao/p/11311199.html

时间: 2024-08-02 07:22:08

LeetCode-三数之和(排序,固定一个数,然后双指针寻找另外两个数)的相关文章

leetcode 三数之和

题目: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 题目分析: 利用循环,使得三数之和为0.分别令i,l,r为三个数,i作为外循环,遍历数组.主要难点在于不重复 参考:https://leetcode.com/problems/3sum/discuss/147561/Python-tm 代码(python): 原文地址:https://ww

LeetCode:最接近的三数之和【16】

LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 题目分析 这道题就是三数之和问题的一种变形. 三数之和问题的求解策略是将三指针变为双指针问题

LeetCode OJ:3Sum Closest(最接近的三数之和)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

LeetCode(15):三数之和

Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ] 解题思路: 这道题让我们求三数之和,比之前那道Two Sum要复杂一些,考虑过先fix一个数,然后另外两个数使

LeetCode第15题 三数之和

/* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]*/ /* 思路: 首先想到的肯定是穷举,时间复杂度为O(n^3) , 但如果使用这种方法,也实在称不上算法题了,果不其然,超时. [

【LeetCode每天一题】3Sum(三数之和)

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums =

[leetcode数组系列]2三数之和

前言 秋招的结束,面试了大大小小的公司,最大的问题在于算法上.所以打算坚持在leetcode打卡,看看到底能不能行,如果你想见证,那我来开车,你坐稳,一起走向更好的远方. 在学习今天内容之前,先学习上一篇的两数之和会更好哟 leetcode两数之和求解 一 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满

[LeetCode] 3Sum Closest 最近三数之和

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

[LeetCode] 16. 3Sum Closest 最近三数之和

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2