题目

给一个包含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) {
// write your code here
if(numbers == null)
return 0;
Arrays.sort(numbers);
int sum = Integer.MAX_VALUE ;
int numlen = numbers.length;
for( int i = 0;i< numlen ;i++){
int left = i + 1;
int right = numlen - 1;
while(left < right){
int tmpsum = numbers[i] + numbers[left] + numbers[right];
int tmpdist = tmpsum - target;
sum = Math.abs(tmpdist) > Math.abs(sum - target) ? sum:tmpsum;
if(tmpdist == 0){
return target;
}
if(tmpdist <0){
left++;
}else{
right--;
} }
}
return sum;
}
}

Java Code

总耗时: 1504 ms

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.
"""
def threeSumClosest(self, numbers, target):
# write your code here
if numbers == None:
return 0
numlen = len(numbers)
numbers.sort()
sum = 0
for i in range(numlen-1):
left = i + 1
right = numlen - 1
while left < right:
tmpsum = numbers[i] + numbers[left] + numbers[right]
tmpdist = tmpsum - target
if i==0:
sum = tmpsum
sum = sum if abs(tmpdist) > abs(sum-target) else tmpsum
if tmpdist == 0:
return target
if tmpdist < 0:
left +=1
else:
right -=1
return sum

Python Code

总耗时: 403 ms

lintcode: 三数之和II的更多相关文章

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

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

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

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

  3. lintcode:三数之和

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

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

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

  5. [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 ...

  6. [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 ...

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

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

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

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

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

随机推荐

  1. module graceful-fs for npm

    问题: 今天使用hexo时发现错误,hexo:command not found.于是重新安装hexo.但是在安装好npm后,却发现运行 npm 出现错误,没有找到模块graceful-fs,在纠结了 ...

  2. Nginx+Center OS 7.2 开机启动设置(转载)

    centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.关 ...

  3. setLayoutParams设置leftMargin后在模拟器上可以真机上不行

    LinearLayout.LayoutParams lp = getLayoutParamsFromExit( (LayoutParams) this.getLayoutParams());      ...

  4. (一)在linux上ubuntu搭建hustOJ系统

    同实验室有人在用java写签到系统,正好我在学习PHP,我就在想能不能在以前学长留下来一直没用OJ上添加一个签到功能. 于是说干就干,就找了许多关于hustoj的文章参考. 首先要说的是安装husto ...

  5. ASP.NET中的TextBox下划线

    看到园子里一位同行写的 http://www.cnblogs.com/xiaopeng84/archive/2007/04/10/707093.html 但是没有贴出效果图,自己练了一下,贴出代码和效 ...

  6. python之递归

    递归的定义:即对自己自身内容的引用. 有用的递归函数应包含以下几步份: 当函数直接返回值时有基本的实例(最小可能性问题): 递归实例,包括一个或者多个问题较小部分的递归调用: 递归的关键就是将问题分解 ...

  7. 第25章 项目6:使用CGI进行远程编辑

    初次实现 25-1 simple_edit.cgi --简单的网页编辑器 #!D:\Program Files\python27\python.exeimport cgiform = cgi.Fiel ...

  8. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. Hadoop管理员的十个最佳实践(转)

    前言 接触Hadoop有两年的时间了,期间遇到很多的问题,既有经典的NameNode和JobTracker内存溢出故障,也有HDFS存储小文件问题,既有任务调度问题,也有MapReduce性能问题.遇 ...

  10. C# 实现Oracle中的数据与Excel之间的转换

    最近项目要求实现数据库之间数据在各个数据库之间导入导出,在此做个笔记 1. 将Oracle中的表导入到Excel中,反之亦然  private static readonly string conne ...