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

 固定一个数,然后从剩余的元素中选择两个。

C++实现代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<climits>
#include<cmath>
using namespace std;

class Solution
{
public:
    int threeSumClosest(vector<int> &num, int target)
    {
        if(num.empty())
            return 0;
        sort(num.begin(),num.end());
        int minSum=0;
        int close=INT_MAX;
        int sum;
        int n=num.size();
        int i;
        for(i=0; i<n-2; i++)
        {
            int left=i+1;
            int right=n-1;
            while(left<right)
            {
                if(num[i]+num[left]+num[right]<target)
                {
                    sum=num[i]+num[left]+num[right];
                    cout<<sum<<endl;
                    if(target-sum<close)
                    {
                        close=target-sum;
                        minSum=sum;
                    }
                    left++;
                }
                else if(num[i]+num[left]+num[right]>target)
                {
                    sum=num[i]+num[left]+num[right];
                    cout<<sum<<endl;
                    if(sum-target<close)
                    {
                        close=sum-target;
                        minSum=sum;
                    }
                    right--;
                }
                else if(num[i]+num[left]+num[right]==target)
                {
                    close=0;
                    //cout<<"i "<<i<<" left "<<left<<" right "<<right<<endl;
                    //cout<<num[i]<<" "<<num[left]<<" "<<num[right]<<endl;
                    minSum=num[i]+num[left]+num[right];
                    return minSum;
                }
            }
        }
        return minSum;
    }
};
int main()
{
    vector<int> vec= {1,2,3,4};
    Solution s;
    int result=s.threeSumClosest(vec,1);
    cout<<result<<endl;
}

运行结果:

 

时间: 2024-09-20 07:42:03

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 = {

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

&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之K sum problem

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

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

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

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

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