题目描述:

  给一个包含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. <学习>.NET的反射基础

    关键词 Assembly 使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例. Module 通过它可以获取包含模块的程序集以及模块中的类等, ...

  2. JavaScript引用类型之Array数组的栈方法与队列方法

    一.栈方法 ECMAScript数组也提供了一种让数组的行为类似与其他数据结构的方法.具体的来说,数组可以变现的向栈一样,栈就是一种可以限制插入和删除向的数据结构.栈是一种LIFO(Last In F ...

  3. Heritrix个性化设置抓取目标

    本文是Heritrix的使用的高级篇,针对对Heritrix已经能够运行的码农朋友们! 我们在抓取网页的时候,网页的链接中往往会包含有js.css.图片.视频等文件,第一次执行抓取任务的时候,许多农民 ...

  4. 用VIM删除空行

    从网上找了一个 :g/^s*$/d 开始用的挺好,后来遇到一种空格开头的空行,就不好用了. MSDN上说正则匹配空行用/^\s*$/,就试着把上面的命令改为: :g/^\s*$/d 就可以了. 用的操 ...

  5. hibernate sql查询后对象转换成实体类

    在多表查询的时候使用hibernate的sql查询的时候,一般返回的是object[]数组,或者可以使用  session.createSQLQuery(sql).setResultTransform ...

  6. Java学习之字符串的创建

    转自:http://lavasoft.blog.51cto.com/62575/80034/ Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时 ...

  7. hdu 4454 Stealing a Cake (三分)

    Stealing a Cake Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. video详解 HTML5中的视频:

    一.video 视频的方法.属性.事件详解 方法:play() 播放  pause() 暂停  属性:currentTime播放到当前的时间   duration视频的总时长 事件:ended 播放完 ...

  9. table常用

    <style> table,table td { border: 1px solid #ccc; border-collapse:collapse; } </style> 注意 ...

  10. 转: Apache SSI详解及应用

    转: Apache SSI详解及应用 什么是 SSI? SSI(Server Side Includes),是嵌套在 HTML 网页中的指示语句,由后台服务器进行代码的解释计算.使用 SSI 可以动态 ...