[leetcode]3 Sum closest
问题叙述性说明:
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).
基本思路:
本题能够利用上一篇《3 Sum》同样的思路来处理。就是对数组排序,然后利用数字之间的序关系降低对不必要情况的处理。
代码:
int threeSumClosest(vector<int> &num, int target) //C++
{
//You may assume that each input would have exactly one solution.
int sum = num[0]+num[1]+num[2];
int dif = abs(sum -target); sort(num.begin(), num.end());
for (int i = 0; i < num.size() - 2;)
{
int l = i + 1, r = num.size() - 1;
while (l < r)
{
int tmpdif = num[l] + num[r] + num[i] - target;
if ( tmpdif <= -dif) l++;
else if (tmpdif > -dif && tmpdif < dif)
{
dif = abs(tmpdif);
sum = num[l] + num[r] + num[i];
if(tmpdif < 0)
do { l++; }while (l < r && num[l - 1] == num[l]);
else if(tmpdif > 0)
do { r--; }while (l < r && num[r + 1] == num[r]);
else return target;
}
else r--;
}
do{ i++; }while (i < num.size() - 1 && num[i - 1] == num[i]);
}
return sum;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[leetcode]3 Sum closest的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- [leetcode]_K Sum 问题
问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- 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 ...
随机推荐
- 根据li标签 查找class="alcw4 alcw41"对应的值
jrhmpt01:/root/lwp/0526# cat a2.pl use LWP::UserAgent; use DBI; use POSIX; use Data::Dumper; use HTM ...
- CButtonEx的实现
要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制.这可以通过定义一个以CButton为基类的新按钮类来实现.以下为具体的实现方法: 方法一: 加入一个新类,类名:CB ...
- js之checkbox的代码全选/全不选,使用id获取元素,而不是name
每当有多个选项的时候,都会有一种想法是:全选,全不选,如果子选项有被选,父选项也得被选. 注意:这里是根据id来获取元素的,但是不能直接用getElementById,因为那只能返回一个,而不是集合. ...
- 双向DFS模板题
B. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Tair是一个高性能,分布式,可扩展,高可靠的key/value结构存储系统(转)
Tair是一个高性能,分布式,可扩展,高可靠的key/value结构存储系统! Tair专为小文件优化,并提供简单易用的接口(类似Map)Tair支持Java和C版本的客户端 Tair is a di ...
- hdu 4706 Children's Day 2013年ICPC热身赛A题 模拟
题意:按字母顺序排列成n型,简单的模拟题. 当字母排到z时从a开始重新排起. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: ...
- QT实现不规则窗体
看到网上有很多不规则窗体的实现,效果很酷.于是使用QT也实现了一个,QT的不规则窗体实现非常简单,只需要设置一个mask(遮掩)图片,这个图片的格式可以使用png或bmp格式,我使用了png格式,默认 ...
- Python语法
- SilkTest Q&A 2
Q11:SilkTest中有没有计算web页面上单词数量的函数? A11:你可以使用Clipboard函数.使用Ctrl+a和Ctrl+c,然后解析string的list. Q12:silktest的 ...
- hnnu 11546 Sum of f(x) (求一个数的全部约数和)
代码: #include<cstdio> #include<cstring> #define N 200000 using namespace std; long long f ...