leetcode 15 三数之和

1.题目

给定一个包含 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2.我的题解

2.1 哈希表解法

  • 固定a
  • 对后续的数据实施twoSum问题解法;双指针(数据要有序)或哈希表(O(N)解法);
  • 总复杂度O(N^2);最后两个用例超时;
  • 虽说哈希表的插入时O(1),但是这里频繁的插入,情况不容乐观;另外去重复的复制、排序等也耗费了时间,这可能是超时的原因;
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int> > res;
        sort(nums.begin(),nums.end());//去重时方便,找到的每一组数下标相对顺序固定,排序后则值固定;

        //find
        int len=nums.size();
        for(int i=0;i<len;i++){
            //a+b+c=0?fix a. 
            //hash table
            unordered_map<int,int> u_map;

            for(int j=i+1;j<len;j++){//b
                //find
                auto it = u_map.find(-nums[i]-nums[j]);//c
                if(it!=u_map.end()){
                    vector<int> temp={nums[i],it->first,nums[j]};//index(a)<index(c)<index(b)
                    res.push_back(temp);
                }
                //insert
                u_map.insert(pair<int,int>(nums[j],j));
            }
        }

        //res  去重复
        set<vector<int> > myset(res.begin(),res.end());
        res.assign(myset.begin(),myset.end());
        return res;
    }
};

2.2 双指针法

  • 双指针法不需要使用map,unordered_map,耗费的时间会少些;
  • 本方法需要先排序;
  • 对于重复的值不要重复固定,否则超时;(最后一个用例是全0)
  • 时间复杂度:O(N^2)
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int> > res;
        sort(nums.begin(),nums.end());//双指针需要有序

        //find
        int len=nums.size();
        int last,first=1;
        for(int i=0;i<len;i++){
            //重复的值不重复固定
            if(!first && nums[i]==last)continue;
            if(first){//第一次没有上一次的值,仅记录当前值。
                last=nums[i];
                first=!first;
            }
            //a+b+c=0?fix a. 
            int l=i+1,r=len-1;
            while(l<r){//find b c
                int cmp=nums[l]+nums[r]+nums[i];
                if(cmp<0)//-->
                    l++;
                else if(cmp>0)
                    r--;
                else{
                    vector<int> temp={nums[i],nums[l],nums[r]};
                    res.push_back(temp);
                    l++;r--;
                }
            }
        }
        //res  去重复
        set<vector<int> > myset(res.begin(),res.end());
        res.assign(myset.begin(),myset.end());
        return res;
    }
};

3.别人的题解

3.1 双指针法

一些提高效率的处理

  • 仅固定负值,这样就不必重复固定;
  • 不固定重复的值,重复的值直接跳过;
  • 双指针法搜索到答案后,指针要一直收缩直到值不重复(两端都是);这样流省去了集合去重复的解法。
  • 时间复杂度:O(N^2)
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int> > res;
        sort(nums.begin(),nums.end());//双指针需要有序

        //find
        int len=nums.size();
        for(int i=0;i<len;i++){
            if(nums[i]>0)break;
            if(i>0 && nums[i]==nums[i-1])continue;
            //a+b+c=0?fix a. 
            int l=i+1,r=len-1;
            while(l<r){//find b c
                int cmp=nums[l]+nums[r]+nums[i];
                if(cmp<0)//-->
                    l++;
                else if(cmp>0)
                    r--;
                else{
                    vector<int> temp={nums[i],nums[l],nums[r]};
                    res.push_back(temp);
                    l++;r--;
                    while(l<r && nums[l]==nums[l-1])l++;
                    while(l<r && nums[r]==nums[r+1])r--;
                }
            }
        }
        //res  去重复
        return res;
    }
};

4.总结与反思

(1)twoSum问题;
(2)双指针法;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值