LeetCode OJ:3Sum 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).
求三个数的和sum,使sum与target相差最小。这题实际上跟前面坐的2Sum,以及3Sum相差不是很大,大体的想法是先排序,然后外层的for循环遍历从小到大的数,然后从剩下的数中取三数之和,然后把最小的一步步保存下来,最终将最小的返回就行了。代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int sz = nums.size();
sort(nums.begin(), nums.end());//当然了,首先还是要排序的
int diff = INT_MAX;//这个直接取INT_MAX,这样计算出来的diff一定是比这个要小的
int tmpDiff;
int ans;
int beg, end;
for (int i = ; i < sz - ; ++i){
beg = i + , end = sz - ;
while (beg < end){
int sum = nums[i] + nums[beg] + nums[end];
if ((tmpDiff = abs(sum - target)) < diff){
diff = tmpDiff;
ans = sum;
}
if (sum > target){
end--;
}
else if (sum < target){
beg++;
}
else
return sum;//相等的话就直接返回了
}
}
return ans;
}
};
大体上就是这样
java版本的如下所示,基本上与上面的没有什么区别:
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int len = nums.length;
int ret = 0;
int diff = Integer.MAX_VALUE;
Arrays.sort(nums);
for(int i = 0; i < len - 2; ++i){
int beg = i + 1;
int end = len - 1;
while(beg < end){
int tmp = nums[i] + nums[beg] + nums[end];
int tmpDiff = Math.abs(tmp - target);
if(tmpDiff < diff){
ret = tmp;
diff = tmpDiff;
}
if(tmp < target)
beg++;
else if(tmp > target)
end--;
else
return target;
}
}
return ret;
}
}
LeetCode OJ:3Sum Closest(最接近的三数之和)的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- Leetcode16.3Sum Closest最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- 016 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.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- lintcode-59-最接近的三数之和
59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...
随机推荐
- (转)VLC播放RTP打包发送的.264文件
VLC播放RTP打包发送的.264文件 1,要有一个发送RTP包的264文件的服务器; 具体代码如下: rtp.h #include <WinSock2.h> #pragma commen ...
- python 各种魔法方法
目录 自定义序列 反射
- 使用pycharm操作django
新建项目,选择已经建立好的虚拟环境 进入指令界面 新建app 添加一些文件和文件夹用于以后存放各种数据 settings设置 TEMPLATES设置 TEMPLATES = [ { 'BACKEND' ...
- 吴超老师课程--HBASE的集群安装
1.hbase的机群搭建过程(在原来的hadoop上的hbase伪分布基础上进行搭建)1.1 集群结构,主节点(hmaster)是hadoop,从节点(region server)是hadoop1和h ...
- 跨平台编译CMake使用
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...
- appium入门基础
1. 建立session时常用命令: DesiredCapabilities cap = new DesiredCapabilities(); cap.SetCapability("brow ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- mount、umount、fuser命令使用小结
mount -t cifs -o username=administrator,password=xxxxx //192.168.11.17/Data/ /mnt/databak/之后想卸载挂载目录, ...
- http://blog.csdn.net/dancing_night/article/details/46698853
http://blog.csdn.net/dancing_night/article/details/46698853
- 2062326 齐力锋 实验四《Java面向对象程序设计Android开发》实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程: 程序设计与数据结构 班级: 1623 姓名: 齐力锋 学号: 20162326 成绩: 指导教师: 娄嘉鹏/王志强 实验日期: 2017年5 ...