LeetCode_3Sum Closest

一.题目

3Sum Closest

Total Accepted: 32191 Total
Submissions: 119262My
Submissions

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).

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题和另外一道题3Sum类似,主要流程也是一样的,也是先进行排序,然后按照顺序选择数组A中的第i个元素作为三个数的最小值来寻找三个数的和,不过的地方在于这里是寻找最靠近给定值,寻找最靠近的值就无所有重复的事情了,所以可以不考虑夹逼的过程中的越过相同元素的过程,虽然越过相同的元素速度会快一些,但是代码长度也会加长。

这道题难的地方可能在于刚开始这种差的阈值的过程,如果把阈值设置得太小了,会出现错误,因此,应该尽可能地将阈值设置得大一点。由于数组是已经排序的,因此,数组中三个数的和的范围在[3*A[0], 3*A[n-1]],因此,阈值可以根据下面三种情况进行设置:

1.if target >= 3*A[n-1],阈值设置为H = target - 3 * A[0];

2.if 3*A[0] <= target<3*A[n-1],阈值设置为H = 3 * A[n-1] - 3*A[0];

3.if target < 3 * A[0],阈值设置为H = 3 * A[n-1] - target。

这样就可以根据阈值与目前得到的三个数的和与target的差来判断是否是最接近target的情况了,根据不同的情况,选择缩放的方向。

三.实现代码

class Solution
{
public:
    int threeSumClosest(vector<int> &num, int target)
    {
        int Size = num.size();

        sort(num.begin(), num.end());

        int MaxSum = 3 * num[Size - 1];
        int MinSum = 3 * num[0];
        int ThreadHold = 0;
        if (target <= MinSum)
        {
            ThreadHold = MaxSum - target;
        }
        if (MaxSum < target)
        {
            ThreadHold = target - MinSum;
        }
        if ((MinSum < target) && (target <= MaxSum))
        {
            ThreadHold = MaxSum - MinSum;
        }

        int Result = 0;

        for (int Index_outter = 0; Index_outter < (Size - 2); Index_outter++)
        {
            int First = num[Index_outter];
            int Second = num[Index_outter + 1];

            if ((Index_outter != 0) && (First == num[Index_outter - 1]))
            {
                continue;
            }

            int Start = Index_outter + 1;
            int End = Size - 1;

            while (Start < End)
            {
                Second = num[Start];
                int Third = num[End];

                int Sum = First + Second + Third;

                if (Sum == target)
                {
                    return Sum;
                }

                if (Sum < target)
                {
                    Start++;
                    if (ThreadHold >= (target - Sum))
                    {
                        Result = Sum;
                        ThreadHold = target - Sum;
                    }
                }

                if (Sum > target)
                {
                    End--;
                    if (ThreadHold >= (Sum - target))
                    {
                        Result = Sum;
                        ThreadHold = Sum - target;
                    }
                }
            }
        }
    return Result;

    }
};

四.体会

这道题最难的地方在于阈值的选择上面,其实可以设置为整数的最大值的,但是,我一开始并不知道如何计算整数的最大值,因此,只能根据排好序的数组的三个数的和的范围与target的关系来设定阈值了,具体的阈值设置情况可以画个数轴出来分析,画出数轴之后,一切就明显了。

版权所有,欢迎转载,转载请注明出处,谢谢

时间: 2024-12-19 07:00:43

LeetCode_3Sum Closest的相关文章

LeetCode 16 3Sum Closest(C,C++,Java,Python)

Problem: 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

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

Leetcode Array 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

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

LeetCode #16 3Sum Closest (M)

[Problem] 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

[LeetCode][JavaScript]3Sum Closest

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 arr

Java [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 =

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

BZOJ3053 The Closest M Points

裸的KD-tree,还是在估计要不要进入子树的时候判断一下就好了,剩下都一样 判断的方法就是看现在答案个数是否小于k,和答案是否会经过分割线. 1 /************************************************************** 2 Problem: 3053 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:2164 ms 7 Memory:3572 kb 8 ************