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(最接近的三数之和)的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. [leetcode]16. 3Sum Closest最接近的三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  4. Leetcode16.3Sum Closest最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  5. 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 ...

  6. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  7. C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...

  8. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  9. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  10. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

随机推荐

  1. Redis一(Redis-py与String操作)

    Redis 介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  2. 关于 sql server 基本使用的建议

    1.   把现有的表插入到新表,(表不能存在),为表备份. --  select  *  into  NewTable   from  OldTable   (NewTable 在select 查询的 ...

  3. for迭代序列的三种方式

    while循环是条件性的,for循环是迭代性的. for循环会访问所有迭代对象中的所有元素,并在所有条目都结束后结束循环. for循环迭代序列有三种基本的方式,分别是通过序列项迭代.通过索引迭代.通过 ...

  4. 配置数据库,Flask-Alchemy

    Flask-Alchemy连接数据库的插件 获取当前项目路径(绝对路径) 来自为知笔记(Wiz)

  5. day4 字符串的使用方法

    一.字符串切片 索引和切片 [起始位置:结束位置:步长] s1 = 'python全栈8期' # 索引从0开始[索引(下标,index)] print(s1[0]) print(s1[3]) prin ...

  6. 练T25- focus必看!所有成功截图汇总

    http://www.guokr.com/post/565880/ 25914人加入此小组 发新帖 练T25- focus必看!所有成功截图汇总! 读图模式 作家向威 作家 2014-02-22 07 ...

  7. 在python中如何使用多进制数字

    我们在python中,除十进制外还可以使用二进制.八进制和十六进制 1.二进制数字由0和1组成,我们使用0b或0B前缀表示二进制数 2.使用bin()函数将一个数字转换为它的二进制形式 print(b ...

  8. 转:Oracle中的日期和字符串互相转换

    TO_DATE格式(以时间:2007-11-02   13:45:25为例)          Year:             yy two digits 两位年                显 ...

  9. 144. Binary Tree Preorder Traversal (二叉树前序遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. 针对Oracle表 列字段的增加、删除、修改以及重命名操作sql

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...