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 ...
随机推荐
- mongodb学习之路1
第一节 MongoDB介绍及下载与安装 引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json的b ...
- Game Tutorials
SDL: http://www.sdltutorials.com/tutorials http://lazyfoo.net/ http://panda3d.noie.name/ http ...
- [转]WPF 依赖项属性
from:http://blog.csdn.net/datoumimi/article/details/8033682 ps:环境限制,发的东西一长就会被拦截,所以删了一部分 UI软件中经常会用到大量 ...
- JDBC ----常用数据库的驱动程序及JDBC URL:
常用数据库的驱动程序及JDBC URL: Oracle数据库: 驱动程序包名:ojdbc14.jar 驱动类的名字:oracle.jdbc.driver.OracleDriver JDBC URL ...
- C/C++ 内联函数
内联函数具备一般函数的性质,但是不需要调用,而是在编译阶段,会用函数体替换函数名被调用的地方.可以节省调用时间(进出栈.保存上下文). 在编译层面和宏的作用相同.内联函数的展开在编译阶段,宏展开在预处 ...
- 标签跳转break和continue
标签是后面跟有冒号的标识符,例如 label1: 在java中,标签起作用的唯一的地方刚好是在迭代语句之前. “刚好之前”的意思表明,在标签和迭代之间置入热和语句都不好. 而在迭代之前设置标签的唯一 ...
- sql2008还原问题
2003的服务器系统不知是因为对SSD的支持不好还是别的原因,系统使用有很多奇怪的问题,所以就升级一下系统,在备份数据库的时候出现了各种问题 源数据库与现数据库都为sqlserver2008,并都安装 ...
- TypeError: Object #<IncomingMessage> has no method 'flash'
JavaScript相关代码: router.post('/reg', function(req, res) { //检验用户两次输入的口令是否一致 if (req.body['password-re ...
- JAVA SSH 框架介绍
SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...
- SVN的405错误
错误1: 如果你没有阅读以下文字,活该你倒霉(这段文字是在googlecode添加新项目时生成的): Command-line access If you plan to make changes, ...