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 = {-   -}, and target = .

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

3 sum 的变形,这里不需要考虑重复而已

class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
int len = num.size();
if(len < ) return ;
int res = num[] + num[] + num[];
for(int i = ; i <= len -; ++i){ int low = i+;
int high = len -;
while(low < high){ int sum = num[i] + num[low] + num[high];
if(sum == target)
return target;
else if(sum < target){
++low;
if(abs(sum - target) < abs(res - target))
res = sum;
}else{
--high;
if(abs(sum - target) < abs(res - target))
res = sum; }
}
} return res;
}
};

LeetCode_3 sum closet的更多相关文章

  1. LeetCode_3 sum

    Given an array S of n integers, are there elements a, b, c ? Find all unique triplets in the array w ...

  2. leetcode16—3 Sum Closet

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  3. Leetcode 3Sum Closest

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

  4. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

  5. leetcode 之Sum系列(七)

    第一题是Two Sum 同样是用哈希表来做,需要注意的是在查打gap是要排除本身.比如target为4,有一个值为2,gap同样为2. vector<int> twoSum(vector& ...

  6. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

随机推荐

  1. VC printf输出彩色字体

    在VC下使用SetConsoleTextAttribute()函数可以改变当前控制台的前景色和背景色,从而达到输出彩色字体的效果. 使用的方法也很简单,具体代码如下: #include <win ...

  2. Android pulltorefresh引用遇到的一个问题

    今天在使用pulltorefresh插件的时候遇到了一个让人头疼的问题,在Eclipse中导入要用到的library项目,然后新建一个项目引入Library,显示的是引入成功,如图 而且project ...

  3. jmeter中webdriver插件,进行自动化压测

    1.下载JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache-jmeter ...

  4. c语言学习之基础知识点介绍(八):函数的基本用法

    本节开始说函数. 一.函数的基本用法  /* 作用:可以实现代码的复用,在一定程度上解决代码冗余的问题:方便后期维护. 语法: void 函数名(){ 函数体; } 函数命名要有意义,遵守驼峰命名法. ...

  5. iOS textfield实现一行的数字限制,超出进行弹框

    步骤一:添加textfield协议‘ @interface LsGeXingQianMingVC ()<UITextFieldDelegate> 步骤2:设置代理 _GeXingQianM ...

  6. 请描述一下 cookies,sessionStorage 和 localStorage 的区别?

    http://handyxuefeng.blog.163.com/blog/static/454521722013111714040259/ http://book.51cto.com/art/201 ...

  7. 将CString(unicode)转换为char*(ANSI)

    1.将CString(unicode)转换为char*(ANSI) CString strServIP; pChat->GetDlgItemText(IDC_IP,strServIP); ] = ...

  8. [lua]尝试一种Case语法糖

    function CaseT(arg) function proxy(caller) caller.yield(r) end -- proxy return function (cond) if (c ...

  9. Shell脚本——中继DHCP服务器自动部署

    详细说明参照: (四)跟我一起玩Linux网络服务:DHCP服务配置之中继代理 vm1的脚本是: #! /bin/bash HIPSEG="10.10.10" SIPSEG=&qu ...

  10. 正则过滤html标签

    var html = "<p>好好学习,<br>天天向上</p>"; var re=/<[^>]+>/g; var text ...