leetcode第16题--3Sum Closest

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

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

吃饭之前刷了这题。思路就是和上题类似。先srot,然后固定i从第一个到倒数第三个,然后设一个left=i+1,right=num.size()-1; 如果三个数相加和target相比较大,那就right--,使三个数相加要变小。如果比target小那就left++。同时用abs记录当前的差值,如果比之前的差值更小,那就记录三个数的和到val中。如果差值为零了,那就直接返回三个数的和。否则到最后在返回val就可以了。代码如下
class Solution {
public:
int threeSumClosest(vector<int> &num, int target)
{
    int left = 0, right = 0, val = 0, minC = INT_MAX;
    sort(num.begin(), num.end());
    for (int i = 0; i < num.size() - 2; ++i)
    {
        left = i + 1; right = num.size() - 1;
        while(left < right)
        {
            int tepVal = target - num[i] - num[left] - num[right];
            if (abs(tepVal) < minC)
                {minC = abs(tepVal); val = num[i] + num[left] + num[right];}
            if (tepVal > 0)
                left++;
            else if (tepVal < 0)
                right--;
            else
                return num[i] + num[left] + num[right];
        }
    }
    return val;
}
};

吃饭去了。

时间: 2024-11-05 22:04:23

leetcode第16题--3Sum Closest的相关文章

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

leetcode_16题——3Sum Closest(两个指针)

3Sum Closest Total Accepted: 38536 Total Submissions: 143223My Submissions Question Solution 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 ma

【LeetCode OJ 016】3Sum Closest

题目链接:https://leetcode.com/problems/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 exact

leetcode第15题--3Sum

Problem: 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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

Array + two points leetcode.16 - 3Sum Closest

题面 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 给定数组,找出并返回最接近target的

Leetcode 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have e

【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

leetCode 16.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

[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