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

思路:

先排序,然后固定一个边界,另外两个边界收缩。

class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int l, r, m;
int ans = num[] + num[] + num[];
sort(num.begin(), num.end()); //数字从小到大排序
for(l = ; l < num.size() - ; l++) //固定左边界
{
m = l + ;
r = num.size() - ;
while(m < r) //收缩中间和右边界
{
int tmp = num[l] + num[m] + num[r];
ans = (abs(ans - target) > abs(tmp - target)) ? tmp : ans;
if(tmp < target)
{
(num[m] > num[r]) ? r-- : m++; //比目标小 则把小的数字变大
}
else if(tmp > target)
{
(num[m] < num[r]) ? r-- : m++; //比目标大 则把大的数字变小
}
else
{
return tmp; //已经等于target就直接返回
}
}
}
return ans;
}
};

【leetcode】3Sum Closest(middle)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. Ghost版Win8.1系统企业版下载

    host版Win8.1系统企业版,下载完成后一定要使用校验工具验证GHO文件MD5值,如果不符请不要安装,不然安装失败后果自负.GHO文件路径一定不要带中文,否则无法安装.安装完成第一次进入桌面会黑屏 ...

  2. Java垃圾回收介绍(译)

    在Java中,对象内存空间的分配与回收是由JVM中的垃圾回收进程自动完成的.与C语言不同的是,在Java中开发者不需要专门为垃圾回收写代码.这是使Java流行的众多特征之一,也帮助了程序员写出了更好的 ...

  3. c#输出json,其中包含子json (可以含 无限级 子json)的方法思路

    首页 给出  DataTable 转Json 的方法: public static string TableToJson(DataTable dt) { List<Dictionary<s ...

  4. [Bootstrap]全局样式(四)

    按钮 1.基本类.btn {display/padding/margin-bottom/font-size/border-radius/border} 作用于< a:role:button &g ...

  5. StringBuilder和StringBuffer

    StringBuilder java.lang 类 StringBuilder java.lang.Object java.lang.StringBuilder 所有已实现的接口: Serializa ...

  6. IIS6,IIS7 最简单的重写URL

    虽然现在很少用IIS6,今天突然要把项目搬到老的服务器上(IIS6),对项目还要重新部署一下. 主要把时间花在了对url的重写上.其实很简单,如下: IIS6 网站 → 属性 → 主目录 → 配置 → ...

  7. AVA取整以及四舍五入

    AVA取整以及四舍五入 import java.math.BigDecimal; public class Test { public static void main(String[] args) ...

  8. 搭建pptpd实现vpn

    PPTP(Point to Point Tuneling Protocol,点对点隧道协议)是一种主要用于VPN的数据链路层网络协议. 环境:debian 7.0 在linux下安装pptpd服务实现 ...

  9. Angular之【form提交问题】

    前端页面是这样: <form class="form-horizontal" role="form" name="LoginForm" ...

  10. Kakfa揭秘 Day6 Consumer源码解密

    Kakfa揭秘 Day6 Consumer源码解密 今天主要分析下Consumer是怎么来工作的,今天主要是例子出发,对整个过程进行刨析. 简单例子 Example中Consumer.java是一个简 ...