LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/
二.题目大意:
给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的和最接近target。
三.题解:
这道题实质就是3sum(http://www.cnblogs.com/wangkundentisy/p/9079622.html)问题的变形,而且题目假设的是解是唯一的,所以该题甚至都不用考虑重复情况了。所以只需在3sum问题中,判断下三个元素的和与target的差值,并根据差值进行赋值即可。
代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
int differ = 0x7fffffff;//初始化差值
int sum = 0;
if(len < 3)
return 0;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i -1])
continue;
int j = i + 1, k = len - 1;
while(j < k)
{
if(nums[i]+ nums[j] + nums[k] == target)
{
return target;//由于题目假设的是解释唯一的,所以如果遇到正好和为target的情况,可以立马返回
}
else if(nums[i] + nums[j] + nums[k] < target)
{
int temp = target - (nums[i] + nums[j] + nums[k]);
if(temp < differ)//找到离target差异最小的一组数据
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
j++;
}
else
{
int temp = (nums[i] + nums[j] + nums[k]) - target;
if(temp < differ)
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
k--;
}
}
}
return sum;
}
};
由于本题基本与3sum问题一致,所以注意事项直接看3sum问题就行了。
LeetCode——16. 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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 ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [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 ...
- 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 ...
- 蜗牛慢慢爬 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 ...
- [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 ...
随机推荐
- cat命令合并多个txt文件
cat是concatenate的缩写,意为串联,之前经常看到别人在用cat命令,没有细究 cat命令两个常用的用法是: cat file.txt能够将txt中的内容显示出来 cat file1.txt ...
- P1220 关路灯 (区间dp)
题目链接:传送门 题目大意: 总共有N盏灯,老张从点C(1 ≤ C ≤ N)开始关灯(关灯不需要等待时间,C点的灯直接关掉),与此同时灯开始烧电(已知功率Pi). 老张每次可以往左走关最近的灯或者往右 ...
- 【HDOJ1069】【动态规划】
http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java/Othe ...
- 浅谈log4j-5-读取properties文件(转自godtrue)
#### 在代码中配置log4j环境的方式,我们已经见识过了,是不是感觉比较麻烦,我们试试使用配置文件的方式是否使您的应用程序更加的灵活.# Log4j支持两种配置文件格式,一种是XML格式的文件,一 ...
- JS字符串和正则总结
trim功能:去除字符串开始和结尾的空格. 中间空格不去掉~ 对输入字符串的处理,多输要先清除开头结尾空格,再处理 IE8不支持trim()方法. String总结:所有API都无法修改原字符串,都会 ...
- 【liunx】linux后台执行命令:&和nohup
当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...
- hibernate中Restrictions的用法
方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge > ...
- 统计cpu相关信息
我的cpu为i3310m 适用类型:笔记本 CPU系列:酷睿i3 3代系列 CPU主频:2.4GHz 三级缓存:3MB 插槽类型:FCBGA1023,FCPGA988 封装大小:37.5×37.5mm ...
- C++中的继承和多继承
一.学习笔记 1.继承 class Student : public Person { ... } 2.继承时权限派生类中可以直接访问父类的protected成员,但是不能访问其private成员,若 ...
- day 49 html 学习 css 学习
画圆 <style> div{width: 100px; height:100px; border: solid red 3px; /*当弧度为.圆角半径为30时 得到的图像*/ bord ...