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. ajax异步请求返回对象

    使用ajax异步请求返回一个对象. java code: @RequestMapping({"getAstSingleWheelImg_bbs"+Constant.JSON}) @ ...

  2. Generating Gaussian Random Numbers(转)

    Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about th ...

  3. NHibernate & INotifyPropertyChanged

    One of the things that make NHibernate easy to use is that it fully support the POCO model. But one ...

  4. And Design:拓荒笔记——Upload上传

    And Design:拓荒笔记——Upload上传 上传前

  5. windows10+mysql8.0.zip安装

    〇.准备: MySQL8.0 Windows zip包下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 环 ...

  6. HDU - 4407 Sum (容斥)

    题意:初始序列[1..N](1<=N<=4e5),支持两种操作:1.求区间[x,y]内与p互素的数之和: 2.将x位置的数变为c. 分析:很容易把人骗到线段树的思维中,而实际上操作2单点的 ...

  7. linux 基础知识总结2

    1. 设定文件text的属性为:文件属主(u) 增加写权限;与文件属主同组用户(g) 增加写权限;其他用户(o) 删除执行权限:chmod ug+w,o-x log2012.log     权限选择参 ...

  8. 20145222黄亚奇《网络对抗》- shellcode注入&Return-to-libc攻击深入

    20145222黄亚奇<网络对抗>- shellcode注入&Return-to-libc攻击深入 shellcode注入实践过程

  9. error: 'for' loop initial declarations are only allowed in C99 mode

    error: 'for' loop initial declarations are only allowed in C99 mode   出现错误: error: 'for' loop initia ...

  10. maven项目包结构

    groupId填写公司名称,如com.enn.ennewartifactId填写项目名称webapps如父工程: <groupId>com.enn.ennew</groupId> ...