LeetCode 016 3SumClosest

探讨了在整数数组中寻找三个数使它们的和最接近给定目标数的问题。通过排序和双指针技术实现高效求解。

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

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 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) {
    }
};
解题思路:
  • 自己的解题思路
参照3Sum做的,按照我之前的思路。
有考虑过Two-Pointers Tech,但是首先想自己的先2后1,发现还是存在同样的问题。之后,再想先1后2,但是当时不知道自己怎么就把这个方法给pass掉了。然后,看了其他人的解法。用这个方法是可以解决的。
说明,自己根本就没有深入领悟Two-Pointers Tech。
此题,用了之前的笨方法。可以通过所有测试案例,但是会超时。
  • 别人的解题思路
先排序,之后利用Two-Pointers Tech,可以加上二叉搜索?
学习收获:
  • 深刻体会到自己的不足;
  • 现在的编程速度还有待提高。整理知识点时,不宜过多。
附件:程序
1、自己的程序:
运用迭代器,进行遍历。遇到重复的,则跳过版。但超时
int threeSumClosest(vector<int>& nums, int target)
{
    if(nums.size() < 3)
    {
        return INT_MAX;
    }
    sort(nums.begin(), nums.end());
    auto p1 = nums.begin();
    auto p2 = p1 + 1;
    int res = *p1 + *p2 + *(p2 + 1);
    while(p1 < nums.end() - 2)
    {
        while(p2 < nums.end() - 1)
        {
            auto p3 = p2 + 1;
            int twoSum = *p1 + *p2;
            int temp = twoSum + *p3;
            int temp1 = temp;
            while(temp < target)
            {
                auto next_p3 = p3 + 1;
                while(next_p3 < nums.end() && (*next_p3 == *p3))
                {
                    ++next_p3;
                }//while4
                p3 = next_p3;
                if(p3 == nums.end())
                {
                    break;
                }
                temp1 = temp;
                temp = twoSum + *p3;
            }//while3
            if(temp == target)
            {
                res = temp;
                break;
            }
            if(abs(temp1 - target) < abs(temp - target))
            {
                temp = temp1;
            }
            if(abs(temp - target) < abs(res - target))
            {
                res = temp;
            }
            auto next_p2 = p2 + 1;
            while(next_p2 < nums.end() - 1 && (*next_p2 == *p2))
            {
                ++next_p2;
            }//while3
            p2 = next_p2;
        }//while2
        if(res == target)
        {
            break;
        }
        auto next_p1 = p1 + 1;
        while(next_p1 < nums.end() - 2 && (*next_p1 == *p1))
        {
            ++next_p1;
        }//while2
        p1 = next_p1;
        p2 = p1 + 1;
    }//while1
    return res;
}
看了下discuss,说不需要考虑重复,所以尝试将重复检车部分去掉。但是,还是超时了。
没有考虑重复情况,直接++迭代器版
int threeSumClosest(vector<int>& nums, int target)
{
    if(nums.size() < 3)
    {
        return INT_MAX;
    }
    sort(nums.begin(), nums.end());
    auto p1 = nums.begin();
    auto p2 = p1 + 1;
    int res = *p1 + *p2 + *(p2 + 1);
    while(p1 < nums.end() - 2)
    {
        while(p2 < nums.end() - 1)
        {
            auto p3 = p2 + 1;
            int twoSum = *p1 + *p2;
            int temp = twoSum + *p3;
            int temp1 = temp;
            while(temp < target)
            {
                ++p3;
                if(p3 == nums.end())
                {
                    break;
                }
                temp1 = temp;
                temp = twoSum + *p3;
            }//while3
            if(temp == target)
            {
                res = temp;
                break;
            }
            if(abs(temp1 - target) < abs(temp - target))
            {
                temp = temp1;
            }
            if(abs(temp - target) < abs(res - target))
            {
                res = temp;
            }
            ++p2;
        }//while2
        if(res == target)
        {
            break;
        }
        ++p1;
        p2 = p1 + 1;
    }//while1
    return res;
}
2、别人的程序
int threeSumClosest(vector<int>& nums, int target)
{
    if(nums.size() < 3) return 0;
    int closest = nums[0] + nums[1] + nums[2];
    sort(nums.begin(), nums.end());
    for(int first = 0; first < nums.size() - 2; ++first)
    {
        if(first > 0 && nums[first] == nums[first - 1]) continue;
        int second = first + 1;
        int third = nums.size() - 1;
        while(second < third)
        {
            int curSum = nums[first] + nums[second] + nums[third];
            if(curSum == target) return curSum;
            if(abs(target - curSum) < abs(target - closest))
            {
                closest = curSum;
            }
            if(curSum > target)
            {
                --third;
            }
            else
            {
                ++second;
            }
        }
    }
    return closest;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值