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 ...
随机推荐
- Windows下 C++ WT +VS2013配置
引出 最近在学习使用C++,另外对建站有点兴趣,所以就找到了WT.对于WT的详细介绍,这里不讲,直接看官网就好. 此文为本人原创,转载请注明出处. 先丢出官网上的干货: WT官方网站: https:/ ...
- Mapxtreme C#鹰眼地图
Demo演示程序下载地址: http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找:EagelEyeMap.r ...
- dojo加载树错误
1.错误叙述性说明 error loading undefined children. TypeError:this._arrayOfTopLevelItems is undefied. ...
- MsSqlServer 语句
--假设 成绩>100 优 --假设成绩>90 良 select * from TblScore select 英语成绩= (case when tEnglish>90 then ...
- Unity3D移动端内存优化(NGUI方面)
Unity3D引擎技术交流QQ群:[21568554] 做3d移动端内存一直是人们头疼的问题,载入的资源释放了,还有其它的须要释放.比方ngui释放,事实上主要是NGUI的Texture和Spr ...
- ActiveMQ与RabbitMQ采用camel综合
著名EIP实施框架Camel它起源于ActiveMQ的一些基于消息的集成需求.然后逐渐发展成为一个ActiveMQ的子项目,最后这一块的功能越来越完好.就成为了Apache的顶级项目. 所以,从一開始 ...
- JavaEE(6) - JMS消息选择和查看
1. JMS消息的类型.消息头和消息属性 消息类型: StreamMessage MapMessage TextMessage ObjectMessage BytesMessage JMS消息中的消息 ...
- Python的print中国输出对齐问题
问题叙述性说明: 在使用Python内置函数print当输出英语,应用格输出类型可以对齐很好: s1 = 'I am a long sentence.' s2 = 'I\'m short.' prin ...
- jsmart 前结合案例
前绑定jsmart这是一个不错的选择.之前通过经常使用的项目中的.最近涉及的领域的后端部.jsmart有些使用相对较少,主要是因为他想引用文件,我写的模板,在一个简单的项目,直接使用js界,很复杂的前 ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...