leetcode第16题--3Sum Closest
Problem: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).
吃饭之前刷了这题。思路就是和上题类似。先srot,然后固定i从第一个到倒数第三个,然后设一个left=i+1,right=num.size()-1; 如果三个数相加和target相比较大,那就right--,使三个数相加要变小。如果比target小那就left++。同时用abs记录当前的差值,如果比之前的差值更小,那就记录三个数的和到val中。如果差值为零了,那就直接返回三个数的和。否则到最后在返回val就可以了。代码如下
class Solution {
public:
int threeSumClosest(vector<int> &num, int target)
{
int left = , right = , val = , minC = INT_MAX;
sort(num.begin(), num.end());
for (int i = ; i < num.size() - ; ++i)
{
left = i + ; right = num.size() - ;
while(left < right)
{
int tepVal = target - num[i] - num[left] - num[right];
if (abs(tepVal) < minC)
{minC = abs(tepVal); val = num[i] + num[left] + num[right];}
if (tepVal > )
left++;
else if (tepVal < )
right--;
else
return num[i] + num[left] + num[right];
}
}
return val;
}
};
吃饭去了。
2015/4/3:
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(), num.end());
int ans, globalVal = INT_MAX;
for (int i = ; i < num.size(); ++i){
if (i > && num[i] == num[i-])
continue;
int left = i+, right = num.size()-;
while(left < right){
int val =target - (num[i] + num[left] + num[right]);
if (abs(val) < globalVal){
ans = num[i] + num[left] + num[right];
globalVal = abs(val);
}
if (val == )
return target;
else if(val > )
left++;
else
right--;
}
}
return ans;
}
};
leetcode第16题--3Sum Closest的更多相关文章
- 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 ...
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- [算法题] 3Sum Closest
题目内容 Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- leetcode第15题--3Sum
Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- android JNI 简单demo(2)它JNI demo 写
android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...
- ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前
原文:ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前 背景 当数据量大.查询条件复杂,多样多的时候,我们可能需要单独做一个查询界面,当用户选择设置了相关的 ...
- 四、Linux/UNIX操作命令积累【chmod、chown、tail】
正在使用Linux/UNIX下一个.经常使用文本界面来设置系统或操作系统,笔者也是在指挥这方面工作的过程中不断的接触.因此,为了此特酝酿.准备.開始了本文的编写.本文主要记录自己平时遇到的一些Linu ...
- 使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历
原文:使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 常常遇到需要向SQL Server插入批量数据,然后在存储过程中对这些数据进行进一步处理的情况.存储过 ...
- FastJson基本使用
在发展中Android的过程中.假设我们常与server联系,更新数据等,然后,json它必须是一个良好的数据格公式,但随着json.使用原生的解析也能够,可是非常不高效,所以这里介绍两种json数据 ...
- ORA-00838: Specified value of MEMORY_TARGET is too small(转)
1. 测试环境OS: RHEL5U5(32bit)DB: Oracle 11.2.0.3.0(32bit) 2. 异常原因. 2.1 oracle 11g默认sga_target为0,如下图, O ...
- 关于在同一母版页中使用多个CSS文件的解决方案
原文:关于在同一母版页中使用多个CSS文件的解决方案 以前都用.NET1.1没遇到这问题,现在换了2.0开始学着使用母版,结果就遇到了这个问题,在百度上一搜索才发现有不少人提出这个问题,但没找到好的解 ...
- JAVA转让JS功能
今天,在发展中使用js和Java互动.通常我们使用更多的是js转让Java方法.可以使用dwr.Ajax.jquery.突然发现Java转让js然后,我真的没见过,今天,互联网提供以下信息,顺便总结: ...
- 单点登录SSO原则的实现
单点登录SSO(Single Sign On)简单地把它是下一个在多系统环境中共存,在用户登录,不要在其他系统上的登录.这是用户第一次登录得到所有其他系统的信任. 单点登录在一个大型网站非常频繁使用, ...
- jQuery本身方法($.each,$.map,$.contains,$ajax)
常用工具方法 (1)$.trim $.trim方法用于移除字符串头部和尾部多余的空格. $.trim(' Hello ') // Hello (2)$.contains $.contains方法返回一 ...