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. GUI之CCControlExtension

    Introduction CCControl is inspired by the UIControl API class from the UIKit library of CocoaTouch. ...

  2. 获取DLL的文件路径以及调用DLL的文件路径

    如何在DLL中,获取DLL本身的路径以及调用DLL的文件的路径呢?主要通过GetModuleFileName(HMODULEhModule,LPTSTR lpFilename,DWORD nSize) ...

  3. Google Map API v2 步步为营 (二)----- Location

    接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...

  4. CentOS7添加第三方源

    CentOS由于很追求稳定性,所以官方源中自带的软件不多,因而需要一些第三方源,比如EPEL.ATrpms.ELRepo.Nux Dextop.RepoForge等. EPEL EPEL即Extra ...

  5. js判断手机端操作系统(Andorid/IOS)

    非常实用的js判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面 androidURL = "http://xxx/xxx.apk"; var browser = ...

  6. Java中OutOfMemoryError(内存溢出)的情况及解决办法

    java.lang.OutOfMemoryError: Java heap space // TODO Auto-generated method stub Vector v = new Vector ...

  7. SQL Server 中WITH (NOLOCK)

    with(nolock)的功能: 1: 指定允许脏读.不发布共享锁来阻止其他事务修改当前事务读取的数据,其他事务设置的排他锁不会阻碍当前事务读取锁定数据.允许脏读可能产生较多的并发操作,但其代价是读取 ...

  8. MyEclipse-java读取jxl的时候报错OutOfMemoryError

    在读取jxl的时候,运行的时候报错: java.lang.OutOfMemoryError: Java heap space     at jxl.read.biff.SSTRecord.<in ...

  9. CI 笔记,使用 json的参考文档(废弃)

    Json的处理转换, Json转换步骤, a)         先设置json为空字符串, b)         While循环,遍历, While(!!$row = mysql_fetch_arra ...

  10. .NET中的委托——摘自MSDN

    封装一个方法,该方法只有一个参数并且不返回值. 命名空间:  System程序集:  mscorlib(在 mscorlib.dll 中) 语法   C#   public delegate void ...