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 = ...
随机推荐
- git rm与直接rm的区别
git rm 行为: 1.删除一个文件 2.将被删除的这个文件纳入缓存区 $ git rm a rm 'a' $ git status On branch master Changes to be c ...
- 你真的会用Retrofit2吗?Retrofit2完全教程
本文注目录: Retrofit入门 Retrofit注解详解 Gson与Converter RxJava与CallAdapter 自定义Converter 自定义CallAdapter 其它说明 前言 ...
- springboot整合JPA创建数据库表失败
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table t_s ...
- vim 设置 颜色值
编辑~/.vimrc文件,添加 set t_Co=8 t_Co即terminal Color之意 注意,将 t_Co 设置为256 (或8以外的所有值) 时,mark 的显示不是很正常.
- kubernetes1.9 手动安装
一.创建TLS证书和秘钥: 1.安装 CFSSL: wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 chmod +x cfssl_linux-amd ...
- 【转】PCA与Whitening
PCA: PCA的具有2个功能,一是维数约简(可以加快算法的训练速度,减小内存消耗等),一是数据的可视化. PCA并不是线性回归,因为线性回归是保证得到的函数是y值方面误差最小,而PCA是保证得到的函 ...
- 多种数据库之间 update的不同
sql server update a set a.gqdltks=b.gqdltks,a.bztks=b.bztks from landleveldata a,gdqlpj b where a.GE ...
- js自执行函数&扩展方法
我们通常将JS代码写在一个单独的JS文件中,然后在页面中引入该文件.但是,有时候引入后会碰到变量名或函数名与其它JS代码冲突的问题.那么如何解决这个问题呢?作用域隔离.在JS中,作用域是通过函数来划分 ...
- nginx 第一天
来公司第一天先装了一下nginx 第一步: 先装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ ...
- HDU 1159 Common Subsequence 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...