题目描述:

  给一个包含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 文件内容转码

    文件内容的转换: iconv -f GB2312 -t UTF-8 gb1.txt >gb2.txt-f, –from-code=名称 原始文本编码-t, –to-code=名称 输出编码-o, ...

  2. IOS开发——正则表达式验证手机号、密码

    App的实际应用中,用户登陆功能基本是每个App都有需求的一个功能.而当前我们很常规的做法,就是让用户把手机号作为自己的用户名,而在注册获取短信验证码的过程中,我们首先要完成的一个步骤,就是校验用户的 ...

  3. B - Moving Tables

    B - Moving Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. Spark源码学习1

    转自:http://www.cnblogs.com/hseagle/p/3664933.html 一.基本概念(Basic Concepts) RDD - resillient distributed ...

  5. 关于Python网络爬虫实战笔记①

    python网络爬虫项目实战笔记①如何下载韩寒的博客文章 python网络爬虫项目实战笔记①如何下载韩寒的博客文章 1. 打开韩寒博客列表页面 http://blog.sina.com.cn/s/ar ...

  6. [LeetCode]题解(python):139-Word Break

    题目来源: https://leetcode.com/problems/word-break/ 题意分析: 给定一个字符串s和一个字典dict,判断s是不是由字典dict里面的元素组成的. 题目思路: ...

  7. IOS 特定于设备的开发:监测Retina支持

    近年来,Apple在其旗舰设备上引入了Retina显示屏.根据Apple的说法,他的像素密度非常高,足以使人眼无法区分单独的像素. UIScreen类提供了一种容易的方式,用于监查当前设备是否提供了内 ...

  8. MySQL show binglog event in 'log_name'

    二进制日志文件记录的内容:记录表的更改. 二进制日志文件记录的形式:基于语句的复制.基于行的复制. 两种记录形式的优点与不足: 基于语句的复制-->它不能保证复制的正确性.如随机函数可能在两台机 ...

  9. MySQL、You are using safe update mode

    MySQL默认模式是sql_safe_updates=1的.在这个模式下不管你是update还是delete一行where 条件都要指定主键.如果where条件只出现的不是主键就会出错. 例子: se ...

  10. Umbraco学习2------数据类型

    一.基础概念 在使用Umbraco这类CMS制作网站之前,先要搞清楚的是,和概念中网站制作的区别. 暂时忘掉所谓的ADO.NET存储.忘掉ASP.NET.忘掉多层架构什么的. 只需要关注:要显示什么. ...