lintcode: 三数之和II
题目
给一个包含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的更多相关文章
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和
[算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -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 < ...
- [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 ...
- [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 ...
- 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 ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- LeetCode第十六题-找出数组中三数之和最接近目标值的答案
3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...
随机推荐
- SQL server数据库内置账户SA登录设置
SQL server数据库内置账户SA登录不了 设置SQL Server数据库给sa设置密码的时候 提示18456 解决步骤: 第二步:右击sa,选择属性: 第三步:点击状态选项卡:勾选授予 ...
- 【原】使用ajax的get异常获取数据的时候,IE浏览器总是有缓存
//HTML里有下面这样一段代码 //异步获取准备人信息 $.get("PrepSetpNew/PrepareMainCrew.ashx?Method=GetPrepUserInfo&quo ...
- 修改UI中的值,无反应解决办法
var targetObj = $("<input name='mydate' class='easyui-datebox'>").appendTo("#id ...
- 每日一“酷”之string
介绍:string模块可以追溯到最早的Python版本中.现在很多的被移植为str和unicode对象的方法,在python3.0中会被完全去除.string模块中,有很多有用的常量和累,用来处理st ...
- JMS之开源实现ActiveMQ
1.ActiveMQ是开源的JMS实现. 可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给jms 服务端,而尽快的把屏幕返还给用户,且服务端能够多线程排队响应高并发的请求.可 ...
- .Net码农学Android---前言
自从毕业参加工作后,就一直想学移动领域得开发,但时间.精力.决心.学习成本等这些问题总在不同程度的阻碍着自己. 但这段时间自己想做一款属于自己的App的想法越来越强烈,我感到自己快压不住这股能量了.终 ...
- IBM MQ Reason 2538(MQRC_HOST_NOT_AVAILABLE) 错误原因一例
环境: .NET 4.0, MQ .NET客户端 IBM.XMS(v2.0.0.3) 测试代码如下: var factoryFactory = XMSFactoryFactory.GetInstanc ...
- 键盘样式风格有关设置-iOS开发
一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKey ...
- Linux C C语言库的创建和调用
C语言库的创建和调用 简介: 假如,你有一个庞大的工程,代码量达到数百兆甚至是数G,你经常会遇到好多重复或常用的地方.每次使用到这些地方时如果都重新写一份基本相同的代码,这当然可以,不过这样会大大地降 ...
- mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
<?php $link=mysql_connect('localhost','root',”); mysql_select_db('abc',$link); $sql = “select * f ...