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>& nums, int target) {
int size = nums.size(); sort(nums.begin(), nums.end());
diff = INT_MAX;
for(int i = ; i < size-; i++){
if(i> && nums[i]==nums[i-]) continue;
find(nums, i+, size-, target-nums[i]);
if(diff == ) return target;
}
return target+diff;
} void find(vector<int>& nums, int start, int end, int target){
int sum;
while(start<end){
sum = nums[start]+nums[end];
if(sum == target){
diff = ;
return;
}
else if(sum>target){
do{
end--;
}while(end!=start && nums[end] == nums[end+]);
if(sum-target < abs(diff)) diff = sum - target;
}
else{
do{
start++;
}while(start!= end && nums[start] == nums[start-]);
if(target - sum < abs(diff)) diff = sum - target; //不能只在最后检查:可能会有这种情况,前一次sum>target,这次sum<target,而且下次就start==end,那么很可能前一次的sum比这次的sum更接近target
}
}
}
private:
int diff; //how much bigger is the sum
};

16.3Sum Closest (Two-Pointers)的更多相关文章

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

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

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

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

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

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

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

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  6. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  7. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  8. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

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

  9. [LeetCode] 16. 3Sum Closest 最近三数之和

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

  10. 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 num ...

随机推荐

  1. New Concept English Two 26 70

    $课文68  纠缠不休 712. I crossed the street to avoid meeting him, but he saw me and came running towards m ...

  2. 第十课 C++异常简介

    异常不是错误,异常是程序中可预料到的另一条执行分支,是可预见的.错误是不可预料的. C++内置了异常处理的语法元素try...catch...,如下: C++通过throw语句抛出异常信息: 上图中t ...

  3. 设计一个栈,设计一个max()函数,求当前栈中的最大元素

    #include <iostream> using namespace std; #define MAXSIZE 256 typedef struct stack { int top; i ...

  4. Codeforces 148B: Escape

    题目链接:http://codeforces.com/problemset/problem/148/B 题意:公主从龙的洞穴中逃跑,公主的速度为vp,龙的速度为vd,在公主逃跑时间t时,龙发现公主逃跑 ...

  5. sublime 下面开发

    sublime 下面开发 开发 ptyon 简单环境 1. 下载sublime 3 https://download.sublimetext.com/Sublime%20Text%20Build%20 ...

  6. Android Studio failed to create error code -6解决方法

    起因是AndroidStudio编译太慢,在stackoverflow上找解决方法,创建了个vm选项文件,导致内存不够用 很多中文博客上都千篇一律地说是内存不够,打开安装路径下bin目录下的studi ...

  7. 配置spring的log4j日志记录

    1.导入依赖包pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  8. 利用spring的CommonsMultipartResolver上传文件

    1.CommonsMultipartResolver是spring里面提供的一个上传方式,效率我不知道,但是加入spring容器管理还是很不错的. 2.先看依赖包pom.xml <project ...

  9. flask 蓝图

    转自:http://spacewander.github.io/explore-flask-zh/7-blueprints.html 蓝图 什么是蓝图? 一个蓝图定义了可用于单个应用的视图,模板,静态 ...

  10. OSG和ProLand 的海面仿真

    基于OSG的海面仿真 OSG中国官网 http://www.osgchina.org/ OSG-ocean的效果图如下 proland的效果图如下 下面为OSG和OCEAN的配置 配置方法转自 htt ...