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

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

【题意】

给定n个整数的数组S,寻找S中三个整数,使得总和最接近给定的目标。

返回三个整数的总和。

你可以假设每个输入将有一个确切的解决方案。

【分析】

题目思路和上题差不多。

先排序,二分查找来左右逼近。

对于a + b + c ,对a规定一个数值时,采用二分查找b,c使Sum= a + b + c 最接近target。

a可以取数组S中任何一个数值,使得有多个Sum,CurClosest = abs(target - Sum)求Min( CurClosest ),即最接近的那个。

【代码】

/*********************************
*   日期:2014-01-18
*   作者:SJF0115
*   题号: 16.3Sum Closest
*   来源:http://oj.leetcode.com/problems/3sum-closest/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <LIMITS.H>
using namespace std;

class Solution {
public:
    int threeSumClosest(vector<int> &num, int target) {
        int i,j,result,start,end,Sum,CurClosest,MinClosest = INT_MAX;
        int Len = num.size();
        //排序
        sort(num.begin(),num.end());
        for(i = 0;i < Len-2;i++){
            start = i + 1;
            end = Len - 1;
            //二分查找
            while(start < end){
                //a + b + c
                Sum = num[i] + num[start] + num[end];
                //接近度
                CurClosest = abs(target - Sum);
                //更新最接近的Target
                if(CurClosest < MinClosest){
                    MinClosest = CurClosest;
                    result = Sum;
                }
                //相等 -> 目标(唯一一个解)
                if(target == Sum){
                    return Sum;
                }
                //大于 -> 当前值小需要增大
                else if(target > Sum){
                    start ++;
                }
                //小于 -> 当前值大需要减小
                else{
                    end --;
                }
            }//while
        }//for
        return result;
    }
};
int main() {
    int result;
    Solution solution;
    vector<int> vec;
    vec.push_back(-1);
    vec.push_back(2);
    vec.push_back(1);
    vec.push_back(-4);
    result = solution.threeSumClosest(vec,-4);
    printf("Target:%d\n",result);
    return 0;
}







目录
相关文章
|
算法 机器学习/深度学习
|
算法 编译器
LeetCode:658. Find K Closest Elements程序分析
好久没有练习算法了,以此纪研究生以来第一次练习算法题。 原题链接:https://leetcode.com/problems/find-k-closest-elements/description/ 题目描述:大概意思是给定一个数组[1,2,3,4,5]和两个常数k,x然后在数组中找到与x最近的的k个元素,找到后的元素依旧按照升序排列。
1723 0
LeetCode - 16. 3Sum Closest
16. 3Sum Closest  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个整数序列和一个目标数,在序列中找出三个数,使得这三个数的和与目标数的差最小.
690 0
|
C++
[LeetCode] Closest Binary Search Tree Value II
Problem Description: Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
1182 0
|
C++
[LeetCode] Closest Binary Search Tree Value
Problem Description: Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
902 0
[LeetCode] 3Sum Closest
This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target.
755 0