[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;
}
时间: 2024-09-17 18:02:55

[LeetCode]16.3Sum Closest的相关文章

LeetCode 16 3Sum Closest(最接近的3个数的和)

翻译 给定一个有n个整数的数组S,找出S中3个数,使其和等于一个给定的数,target. 返回这3个数的和,你可以假定每个输入都有且只有一个结果. 例如,给定S = {-1 2 1 -4},和target = 1. 那么最接近target的和是2.(-1 + 2 + 1 = 2). 原文 Given an array S of n integers, find three integers in S such that the sum is closest to a given number,

LeetCode 15 3Sum(3个数的和)

翻译 给定一个有n个整数的数组S,是否存在三个元素a,b,c使得a+b+c=0? 找出该数组中所有不重复的3个数,它们的和为0. 备注: 这三个元素必须是从小到大进行排序. 结果中不能有重复的3个数. 例如,给定数组S={-1 0 1 2 -1 4},一个结果集为: (-1, 0, 1) (-1, -1, 2) 原文 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F

[LeetCode]15.3Sum

[题目] 3Sum  Total Accepted: 6032 Total Submissions: 35898My Submissions 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 tri

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

LeetCode 18 4Sum(4个数的和)

翻译 给定一个有n个数字的数组S,在S中是否存在元素a,b,c和d的和恰好满足a + b + c + d = target. 找出数组中所有的不想等的这四个元素,其和等于target. 备注: 在(a,b,c,d)中的元素必须从小到大排列.(a ≤ b ≤ c ≤ d) 其结果必须不能够重复. 例如,给定S = {1 0 -1 0 -2 2},target = 0. 一个结果集为: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) 原文 Given an ar

&lt;font color=&quot;red&quot;&gt;[置顶]&lt;/font&gt;

Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,本博客会持续更新,感谢您的支持,欢迎您的关注与留言.博客有多个专栏,分别是关于 Windows App开发 . UWP(通用Windows平台)开发 . SICP习题解 和 Scheme语言学习 . 算法解析 与 LeetCode等题解 . Android应用开发 ,而最近会添加的文章将主要是算法和Android,不过其它内容也会继续完善. About the Author 独立 Windows App 和

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

LeetCode之K sum problem

做过leetcode的人都知道, 里面有2sum, 3sum(closest), 4sum等问题, 这些也是面试里面经典的问题, 考察是否能够合理利用排序这个性质, 一步一步得到高效的算法. 经过总结, 本人觉得这些问题都可以使用一个通用的K sum求和问题加以概括消化, 这里我们先直接给出K Sum的问题描述和算法(递归解法), 然后将这个一般性的方法套用到具体的K, 比如leetcode中的2Sum, 3Sum, 4Sum问题. 同时我们也给出另一种哈希算法的讨论. leetcode求和问题