https://leetcode.com/problems/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).

思路:

和上一道差不多,主要还是有序数组里两个flag移动。

AC代码:

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res,t,j,k,lowerbound=INT_MAX,n=nums.size();
sort(nums.begin(),nums.end());
for(int i=;i<n-;i++){
t=target-nums[i];
j=i+;
k=n-;
while(j<k){
if(abs(nums[j]+nums[k]-t)<lowerbound){
res=nums[i]+nums[j]+nums[k];
lowerbound=abs(nums[j]+nums[k]-t);
}
if(nums[j]+nums[k]<t)
j++;
else
k--;
}
}
return res;
}
};

LeetCode(16)题解--3Sum Closest的更多相关文章

  1. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

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

  3. leetcode第16题--3Sum Closest

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

  4. LeetCode题解——3Sum Closest

    题目: 给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和. 解法: 和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径. 代码: class Solu ...

  5. LeetCode OJ:3Sum Closest(最接近的三数之和)

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

  6. 【LeetCode】016 3Sum Closest

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

  7. leetcode笔记:3Sum Closest

    一.题目描写叙述 二.解题技巧 该题与3Sum的要求类似.不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路类似,先是进行排序.然后顺序选择数组A中的 ...

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

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

随机推荐

  1. best corder MG loves gold

    MG loves gold  Accepts: 451  Submissions: 1382  Time Limit: 3000/1500 MS (Java/Others)  Memory Limit ...

  2. TroubleShoot: SharePoint 2013: ExecuteOrDelayUntilScriptLoaded 页面发布后不执行的问题

    SharePoint 2010 中的ExecuteOrDelayUntilScriptLoaded,在2013 中使用时没有效果的问题. Example: SharePoint 2013 Code: ...

  3. UVa1362 Exploring Pyramids

    区间dp,枚举走完第一个子树之后回到根节点的位置. /*by SilverN*/ #include<algorithm> #include<iostream> #include ...

  4. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  5. [专题总结]数位DP

    总结: 1:第i个数符合要求了,所以接下来的数都可以.如果没限制, 那么是有  10i-1  个.如果有限制,那么是   (nowx % 10i-1)+1  . 2:两种状态设置 有设状态d      ...

  6. delphi中关于时间差的实例

    http://www.cnblogs.com/rogee/archive/2010/09/20/1832035.html 很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下: 首先 us ...

  7. echarts 金字塔

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. luogu P1197 [JSOI2008]星球大战

    题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道 ...

  9. bubble chat listview

    最近在iOS中用到bubble chat listview,找了个比较有名气的lib(MessagesTableViewController)=>https://github.com/jesse ...

  10. 给button添加长按手势并侦测到此button

    1, 添加手势 self.longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@ ...