LeetCode Two Sum、3Sum、3Sum Closest、4Sum

本文探讨了给定整数数组中寻找特定和的问题,包括两数之和、三数之和、最接近目标的三数之和及四数之和等算法实现。通过使用C++,提出了高效解决方案,并避免重复结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        map<int,int>mp;
        vector<int>res;
        int len = nums.size();
        for(int i=0;i<len;i++)
            mp[nums[i]]=i;
        for(int i=0;i<len;i++)
        {
            if(mp[target-nums[i]])
            {
                res.push_back(i);
                res.push_back(mp[target-nums[i]]);
                return res;
            }
        }
    }
};

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: 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]
]
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int len=nums.size();
        vector<vector<int> > res;
        if(len<=2) return res;
        sort(nums.begin(),nums.end());
        for(int i=0;i<len;)
        {
            int st=i+1,ed=len-1;
            while(st<ed)
            {
                if(nums[i]+nums[st]+nums[ed]==0)
                {
                    res.push_back({nums[i],nums[st],nums[ed]});
                    st++;
                    ed--;
                    while(st<ed&&nums[st]==nums[st-1]) st++;
                    while(st<ed&&nums[ed]==nums[ed+1]) ed--;
                }
                else if(nums[i]+nums[st]+nums[ed]<0)
                {
                    st++;
                    while(st<ed&&nums[st]==nums[st-1]) st++;
                }
                else if(nums[i]+nums[st]+nums[ed]>0)
                {
                    ed--;
                    while(st<ed&&nums[ed]==nums[ed+1]) ed--;
                }
            }
            i++;
            while((i<len)&&(nums[i]==nums[i-1]))
                i++;
        }
        return res;
    }
};

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 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int len=nums.size();
        sort(nums.begin(),nums.end());
        int res=nums[0]+nums[1]+nums[2];
        if(len==3) return res;
        for(int i=0;i<len-2;i++)
        {
            int st=i+1,ed=len-1;
            while(st<ed)
            {
                int sum=nums[i]+nums[st]+nums[ed];
                if(abs(sum-target)<abs(res-target))
                {
                    res=sum;
                    if(res==target)
                        return res;
                }
                if(sum>target)
                    ed--;
                else
                    st++;
            }
        }
        return res;
    }
};

Given an array S of n integers, are there elements abc, 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]
]
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;
        int len=nums.size();
        if(len<4) return res;
        sort(nums.begin(),nums.end());
        for(int i=0;i<len-3;i++)
        {
            if(i>0&&nums[i]==nums[i-1]) continue;
            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
            if(nums[i]+nums[len-1]+nums[len-2]+nums[len-3]<target) continue;
            for(int j=i+1;j<len-2;j++)
            {
                if(j>i+1&&nums[j]==nums[j-1]) continue;
                if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
                if(nums[i]+nums[j]+nums[len-1]+nums[len-2]<target) continue;
                int st=j+1,ed=len-1;
                while(st<ed)
                {
                    int sum=nums[i]+nums[j]+nums[st]+nums[ed];
                    if(sum==target)
                    {
                        res.push_back({nums[i],nums[j],nums[st],nums[ed]});
                        ed--;
                        while(st<ed&&nums[ed]==nums[ed+1])
                            ed--;
                        st++;
                        while(st<ed&&nums[st]==nums[st-1])
                            st++;
                    }
                    else if(sum>target)
                    {
                        ed--;
                        while(st<ed&&nums[ed]==nums[ed+1])
                            ed--;
                    }
                    else if(sum<target)
                    {
                        st++;
                        while(st<ed&&nums[st]==nums[st-1])
                            st++;
                    }
                }
            }
        }
        return res;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值