[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 ...
随机推荐
- Possible concurrency problem: Replicated version id X matches in-memory version for session ...
The message basically is saying that a replicated session is overriding an existing session in that ...
- dataStage 7.5.1A
------------------------------ DataStage Server License ------------------------------ Serial Num ...
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
http://blog.csdn.net/swimmer2000/archive/2007/10/30/1856213.aspx MFC(VC6.0)的CWnd及其子类中,有如下三个函数: / ...
- 也谈C#之Json,从Json字符串到类代码
原文:也谈C#之Json,从Json字符串到类代码 阅读目录 json转类对象 逆思考 从json字符串自动生成C#类 json转类对象 自从.net 4.0开始,微软提供了一整套的针对json进 ...
- VS2010 编译安装boost库
实践是最好的办法..学习C++,想试试线程,然后打算用boost库,结果boost库编译差点吓到我..没看到比较完整的安装教程..一直耽搁.今天动手.完成了.方法记录如下:1.下载boost从boos ...
- MFC 直线 虚线 折线 圆 椭圆 矩形 弧形
****Dlg.h头文件加入: //为project加入画笔.点变量数组 public: CPen m_pen[5]; CPoint m_point[5]; public: void DrawLine ...
- 积累的VC编程小技巧之对话框
1.用鼠标移动基于对话框的无标题栏程序的简单方法 void CVCTestDlg::OnLButtonDown(UINT nFlags, CPoint point) { //一句话解决问题 ...
- java中subString、split、stringTokenizer三种截取字符串方法的性能比较(转)
最近在阅读java.lang下的源码,读到String时,突然想起面试的时候曾经被人问过:都知道在大数据量情况下,使用String的split截取字符串效率很低,有想过用其他的方法替代吗?用什么替代? ...
- Oracle 执行计划了的rows概念
alter session set statistics_level=all; select t1.* from t1,t2 where t1.id=t2.id and t1.id<3; sel ...
- python 时间戳 datetime string 转换
import datetime import time **datetime转时间戳** In [1]: now = datetime.datetime.now() In [2]: time.mkti ...