题目描述:

  给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和。

注意事项

  只需要返回三元组之和,无需返回三元组本身

样例

  例如S = [-1, 2, 1, -4] and target = .  和最接近1的三元组是 -1 + 2 + 1 = 2.

 public class Solution {
/**
* @param numbers: Give an array numbers of n integer
* @param target : An integer
* @return : return the sum of the three integers, the sum closest target.
*/
public int threeSumClosest(int[] numbers ,int target) {
// 记录最小的差值
long minDiff = Long.MAX_VALUE;
// 记录最小差值对应的三个整数和
long result = 0;
// 每次求得的差值
long diff;
// 每次求得的三个整数的和
long sum; // 先对数组进行排序
Arrays.sort(numbers); // i表示假设取第i个数作为结果
for (int i = 0; i < numbers.length - 2; i++) {
// 第二个数可能的起始位置
int j = i + 1;
// 第三个数可能是结束位置
int k = numbers.length - 1; while (j < k) {
// 求当前三个数的和
sum = numbers[j] + numbers[k] + numbers[i];
// 当前和与目标和之间的差值
diff = Math.abs(target - sum); // 差值为0就直接返回
if (diff == 0) {
return (int) sum;
} // 如果当前的差值比之前记录的差值小
if (diff < minDiff) {
// 更新最小的差值
minDiff = diff;
// 更新最小差值对应的和
result = sum; // 以上的设置在下一次元素处理时生效
} // 和大于target
if (sum > target) {
k--;
}
// 和小于target
else {
j++;
}
}
} return (int) result;
}
}

LintCode-三数之和 II的更多相关文章

  1. lintcode: 三数之和II

    题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = .  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...

  2. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  3. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  4. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  5. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  6. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  8. 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 num ...

  9. LeeCode数组第15题三数之和

    题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  10. LeetCode第十六题-找出数组中三数之和最接近目标值的答案

    3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...

随机推荐

  1. 推荐两个不错的CAD二次开发(.Net)手册

    推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...

  2. C# 想要程序文件移动 而数据保持相对位置

    如果用的数据库是access数据库 可以把数据库文件放到bin\debug下面,引用相对位置就可以了 如果程序中有上载文件,而程序需要使用到该文件,那么我们最好也是引用相对文件,我们只需要在数据表中的 ...

  3. js中字符串方法

    字符串方法: 1. charAt(索引值)//通过索引值获取字符串中对应的值 例如: var str='sdf123'; alert(str.charAt(0));//结果弹出第一个索引对应的值:s

  4. 调度器(scheduler)

    调度器(schedule)为游戏提供定时事件和定时调用服务. 调度器(schedule)的功能和事件监听器(eventlistener)的功能有点类似:都是在特定情况下调用某个事先准备好的回调函数. ...

  5. BZOJ 1951: [Sdoi2010]古代猪文( 数论 )

    显然答案是G^∑C(d,N)(d|N).O(N^0.5)枚举N的约数.取模的数999911659是质数, 考虑欧拉定理a^phi(p)=1(mod p)(a与p互质), 那么a^t mod p = a ...

  6. 【转】Eclipse自动补全的设置方法

    转自:http://blog.csdn.net/xiadasong007/archive/2009/11/11/4799715.aspx 打开 Eclipse -> Window -> P ...

  7. MYSQL this function has none of deterministic no sql ......错误

    This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...

  8. Windows 系统消息范围和前缀,以及消息大全

    Windows系统定义的消息类别消息标识符前缀 消息分类ABM 应用桌面工具栏消息BM 按钮控件消息CB 组合框控件消息CBEM 扩展组合框控件消息CDM 通用对话框消息DBT 设备消息DL 拖曳列表 ...

  9. linux杂谈(十七):iscsi存储分离技术

    1.iscsi简单介绍 ​ ​iSCSI利用了TCP/IP的port 860 和 3260 作为沟通的渠道.透过两部计算机之间利用iSCSI的协议来交换SCSI命令,让计算机能够透过快速的局域网集线来 ...

  10. 学习pthreads,给线程传递多个參数

    上篇博文中.boss线程给其它线程传递的仅仅有一个參数,那么假如是多个參数呢?怎么传递呢?也许你会有这种疑问,带着这个疑问,我们进入本文的世界,这里传递多个參数,採用结构体,为什么呢?由于结构体里能够 ...