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. Paxos算法之旅(四)zookeeper代码解析--转载

    ZooKeeper是近期比较热门的一个类Paxos实现.也是一个逐渐得到广泛应用的开源的分布式锁服务实现.被认为是Chubby的开源版,虽然具体实现有很多差异.ZooKeeper概要的介绍可以看官方文 ...

  2. 浅谈Android自定义锁屏页的发车姿势

    一.为什么需要自定义锁屏页 锁屏作为一种黑白屏时代就存在的手机功能,至今仍发挥着巨大作用,特别是触屏时代的到来,锁屏的功用被发挥到了极致.多少人曾经在无聊的时候每隔几分钟划开锁屏再关上,孜孜不倦,其酸 ...

  3. oracle常用数据类型

    oracle中常用数据类型分为三大类:

  4. windows向ubuntu过渡之常用编程软件安装

    不出意外的上篇文章又被踢出首页了,心情甚是悲桑..希望更多人能看到 1.安装codeblocks 直接在软件中心搜索codeblocks就可以 2.安装jdk并配置环境变量 http://www.li ...

  5. sqlmap

    http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注 ...

  6. js常用笔记

    此文仅为笔记,代码来源自网络 网络技术文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript 1.使用||和&&简化语句, ...

  7. xml--小结②XML的基本语法

    二.XML的基本语法1.文档声明:作用:用于标识该文档是一个XML文档.注意事项:声明必须出现在文档的第一行(之前连空行都不能有,也不能有任何的注释) 最简单的XML声明:<?xml versi ...

  8. js--小结④

    举例子,一个demo.提醒自己经常性会在onclick 和function()这两个地方出错 onclick会输少一个字母 function会忘记输括号

  9. js使用

    js使用 HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head&g ...

  10. [Twisted] deferred

    Twisted提供一个优雅的实现(Deferred)来管理回调函数. Deferred Object 的结构 Deferred Object包含两个回调函数列表.一个用来保存成功的回调函数,另一个用来 ...