题目

给一个包含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. .NET研发人员面试题(二)

    1.当使用new BB()创建BB的实例时,产生什么输出? public class AA { public AA() { PrintFields(); } public virtual void P ...

  2. 【原】使用ajax的get异常获取数据的时候,IE浏览器总是有缓存

    //HTML里有下面这样一段代码 //异步获取准备人信息 $.get("PrepSetpNew/PrepareMainCrew.ashx?Method=GetPrepUserInfo&quo ...

  3. PHP中文URL编解码(urlencode()rawurlencode()

    PHP中对于URL进行编码,可以使用 urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20',不过应该注意的是,在编码时应该只 ...

  4. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

  5. 查找指定表的字段not null约束,并生成删除Sql

    SElECT 'ALTER TABLE '+OBJECT_NAME(c.parent_obj)+' DROP CONSTRAINT '+ c.name FROM sys.sysconstraints ...

  6. smarty框架块函数

    块函数的形式是这样的:{func} .. {/func}.换句话说,它们被封闭在一个模板区域内,然后对该区域的内容进行操作.默认地,你的函数实现会被Smarty调用两次:一次是在开始标签,另一次是在闭 ...

  7. How to modify Code Comments[AX2012]

    // This is a framework class. Customizing this class may cause problems with future upgrades to the ...

  8. Application,Session和Cookie

    做ASP.NET,肯定会和这几个对象打交道,这些也是基础面试的常见题目,总结一下还是必要的,好在大神已经总结好了,直接参考就好了: http://www.cnblogs.com/breezeblew/ ...

  9. Environment variable ORACLE_UNQNAME not defined. Please set ORACLE_UNQNAME to database unique name. 的解决方法

    环境:Oracle 11g r2   win7 问题描述:Environment variable ORACLE_UNQNAME not defined. Please set ORACLE_UNQN ...

  10. hdu 2837 Calculation 指数循环节套路题

    Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...