题目描述:

  给一个包含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. linux 使用sudo开放普通用户权限

    整理一下以前写的东东,刚才又忘了- ---------------------------------------------------------------------------------- ...

  2. asp.net MVC Razor 语法(1)

    Razor 不是编程语言.它是服务器端标记语言. 什么是 Razor ? Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时, ...

  3. hahahahah

    dsfsefesfsffsfsfsfsfesfsfsfsfsfsfspackage realm;   import java.util.ArrayList; import java.util.List ...

  4. onhashchange事件--司徒正美

    onhashchange事件是针对AJAX无缝刷新导致后退键失效而产生的事件,因此属于一个够新的事件,浏览器兼容性如下: Feature Chrome Firefox IE Opera Safari ...

  5. IE 11 无法访问某些不兼容性视图的解决方法

    今天下午部署公司的项目时,用IE 11只能加载到JSP页面的静态元素,其中下拉文本框的信息获取不到, 后来,发现是IE 11不兼容的原因,于是,在菜单条“工具”——“兼容性视图设置”,将不兼容页面的网 ...

  6. pushViewController自定义动画

    实现的主要代码如下: CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transiti ...

  7. linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page

    [Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...

  8. ThinkPHP第二十一天(JQuery元素获取,parents属性,toggle方法,确认弹出对话框使用)

    1.JQuery用法 A:$(function(){code...});表示当页面载入完毕后执行 B:获取元素方法:根据class名称 $('.classname'),根据ID名称 $('#IDnam ...

  9. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  10. .NET 条件查询实现--类似网上商城宝贝搜索

    需要实现的效果:点击表格列头:弹出一个层,用户可以输入当前列头的查询条件,点击确定之后,把该列头的查询信息显示在页面顶部,用户可以叉掉这个查询条件,恢复到查询之前的数据. 大致实现的效果图: 项目背景 ...