LeetCode 15.3Sum 18.4Sum 对撞指针 ****

题目 15-3Sum

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: 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个元素的整型数组,它们中的有元素a,b,c。使得a+b+c = 0.返回所有不同的三元组a,b,c.

注意

  • 怎么样才能算不同的三元组
  • 无解怎么办
  • 如果有多解,返回的顺序

思路

和1.2Sum的解法类似,使用对撞指针.
首先对nums进行排序,该题就退化成a+b=-c,遍历数组target = -nums[i],使用对撞指针查找a,b。使得a+b=target,代码中体现为nums[l]+nums[r]=target
这里需要注意,遍历一次相当于查找了含有target所有的可能,因此在之后对撞查找中不需要从头开始,否则就有重复解。
有三处优化:
1.当num[i]和nums[i+1]相等直接跳过,原因已经说过,含target的可能已经查找完毕。
2.当对撞指针左边指针是一个解时,如果nums[l]和nums[l+1]相等直接跳过,因为target不变,如果nums[l+1]不变,那么nums[r]也不变,重复解
e.g:[-1,-2,-2,-2,3,3,3] 当三个指针是-1,-2,3时,之后需要跳过两个-2和两个3否则会有重复解。
3.对撞指针右侧的优化类似
这里写图片描述

代码

//时间复杂度O(n^2)
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        //对撞指针的方法可以退化为 Two sum
        sort(nums.begin(),nums.end());
        vector<vector<int>> vecres;
        for (int i = 0; i < nums.size(); i++)
        {
            int target = -nums[i];
            int l = i + 1;
            int r = nums.size() - 1;
            while(l<r)
            {
                if (target == (nums[l] + nums[r]))
                {
                    int res[3] = { nums[l], nums[r], nums[i] };
                    vecres.push_back(vector<int>(res, res + 3));

                    while(l< r && nums[l] == res[0])
                        l++;
                    while(l<r && nums[r] == res[1])
                        r--;
                }
                else if(target > (nums[l]+nums[r]))
                {
                    l++;
                }
                else
                {
                    r--;
                }
            }
            while(i+1<nums.size()&&nums[i+1] == nums[i])
                i++;
        }
        return vecres;
    }
};

结果

这里写图片描述

题目 18-4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[[-1, 0, 0, 1],[-2, -1, 1, 2], [-2, 0, 0, 2]]

题意

给定一个含有n个元素的整型数组,它们中的有元素a,b,c,d。使得a+b+c+d= target.返回所有不同的四元组a,b,c,d

注意

  • 怎么样才能算不同的四元组
  • 无解怎么办
  • 如果有多解,返回的顺序

思路

和3sum类似,只是需要转化一下思维
1.a+b+c+d=taget转为3Sum:a+b+c=target-d
2.转为2sum:a+b=target-c-d
外围两重循环求出c,d的所有组合,a和b使用对撞指针确定。

代码

//时间复杂度:O(n^3)
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;        
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size();i++)
        {
           int d = target - nums[i];
            for(int j=i+1;j<nums.size();j++)
            {
               int c = d-nums[j];
                int l = j+1;
                int r = nums.size()-1;
                while(l<r)
                {
                    if(c == (nums[l]+nums[r]))
                    {
                        int ires[4]={0};
                        ires[0] = nums[i];
                        ires[1] = nums[j];
                        ires[2] = nums[l];
                        ires[3] = nums[r];

                        res.push_back(vector<int>(ires,ires+4));
                        while(l<r&&nums[l]==ires[2])
                            l++;
                        while(l<r&&nums[r]==ires[3])
                            r--;
                    }
                    else if(c > (nums[l]+nums[r]))
                    {
                        l++;
                    }
                    else
                    {
                        r--;
                    }
                }
                while(j+1<nums.size()&&nums[j] == nums[j+1])
                    j++;
            }
            while(i+1<nums.size()&&nums[i] == nums[i+1])
                i++;
        }
        return res;
    }
};

结果

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值