Leetcode 16 3Sum Closest

原创 2016年08月29日 14:26:12

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

和15题差不多,做一些简单的变形就行了

15题题解链接 http://blog.csdn.net/accepthjp/article/details/52347224

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        int result=0,d=INT_MAX;
        for(int i=0;i<(int)nums.size()-2;i++)
        {
            if(i!=0 && nums[i]==nums[i-1])continue;
            int l=i+1,r=(int)nums.size()-1;
            while(l<r)
            {
                int com=(nums[i]+nums[l]+nums[r]);
                if(com<=target)
                {
                    l++;
                   // while(l<r && nums[l]==nums[l-1]) l++;
                    if(target-com<d)
                    {
                        d=target-com;
                        result=com;
                    }
                }
                else
                {
                    r--;
                   // while(l<r && nums[r]==nums[r+1]) r--;
                    if(com-target<d)
                    {
                        d=com-target;
                        result=com;
                    }
                }
            }
        }
        return result;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

16. 3Sum Closest LeetCode题解

Given an array S of n integers, find three integers in S such that the sum is closest to a given num...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

[LeetCode16]3Sum Closest

3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t...

[leetcode: Python]16. 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given num...
  • NXHYD
  • NXHYD
  • 2017-05-18 16:39
  • 63

leetcode 16 3Sum Closest

问题https://leetcode.com/problems/3sum-closest/解法class Solution { public: int threeSumClosest(vect...

【leetcode】Array——3Sum Closest(16)

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a gi...

LeetCode 16_3Sum Closest

今天抽了点儿时间,来刷一道。 这是第16题了,这连续几道题的题型都比较类似,都是要求对数组排列组合,然后从中找出符合特定条件的一些组合,这种题算是一种比较大的题型吧,很多题目最终都转化成了这种题目,...

LeetCode 16. 3Sum Closest

LeetCode 16. 3Sum Closest题目要求:给出一个整型数组nums和一个整数target,要求从nums中找出3个整数a,b,c使得它们的和最接近target。 题目给出的框架如下...

[leetcode 16] 3Sum Closest

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a ...

《LeetBook》leetcode题解(16):3Sum Closest [M]

16. 3Sum Closest [M]Given an array S of n integers, find three integers in S such that the sum is cl...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)