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.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

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

解题思想

这是“三数之和”问题的扩展,解法类似,先对数组排序,采用双指针的思想,从首尾往中间扫描,唯一的区别是在遍历过程中,增加一个变量来记录遍历过程中最接近target的值。

解题代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        if(nums.size() < 3)
            return 0;
        sort(nums.begin(), nums.end());
        int closet = nums[0] + nums[1] + nums[2];
        for(int i = 0; i < nums.size(); i++){
            int start = i + 1;
            int end = nums.size() - 1;
            while(start < end){
                int sum = nums[i] + nums[start] + nums[end];
                if(sum == target)
                    return sum;
                else if(sum < target)
                    start++;
                else
                    end--;
                if(abs(sum - target) < abs(closet - target))
                    closet = sum;
            }
        }
        return closet;
    }
};

int main()
{
    Solution solution;
    vector<int> nums = {-1, 2, 1, -4};
    int target = 1;
    cout << solution.threeSumClosest(nums, target) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013507678/article/details/80502263
今日推荐