3 sum closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
找到数组中跟目标数最接近的三个数的和 。
这里也是三个数的和问题,经过上一题三个数的和,这一题结果只有一个,也会有重复数字出现,需要处理,当然了,这里只要找到近似的和,不处理重复不影响结果。。
这里也是遍历数组,然后在剩下的数组中依次遍历(两端向中间),每次遍历都会产生一个三数之和,如果该和大于target,则需要右边指针向左移动,反之左边向右移动。同时也要判断此时的和与target之间的距离跟result与target之间距离的大小。
class Solution {
public int threeSumClosest(int[] nums, int target) {
/**
这里也是三个数的和问题,经过上一题三个数的和,这一题结果只有一个,也会有重复数字出现,需要处理,当然了,这里只要找到近似的和,不处理重复不影响结果。。
这里也是遍历数组,然后在剩下的数组中依次遍历(两端向中间),每次遍历都会产生一个三数之和,如果该和大于target,则需要右边指针向左移动,反之左边向右移动。同时也要判断此时的和与target之间的距离跟result与target之间距离的大小。
*/
//初始一个三个元素的和的变量,下面计算三个数的和时要和这个以及target之间的差值作比较
int result=nums[0]+nums[1]+nums[nums.length-1];
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
//两个数的和与sum最接近就行
int left=i+1,right=nums.length-1;
while(left<right){
int sum=nums[i]+nums[left]+nums[right];
if(sum>target) right--;
else left++;
if(Math.abs(result-target)>Math.abs(sum-target))
result=sum;
}
}
return result;
}
}
3 sum closest的更多相关文章
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- 3 Sum Closest 解答
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- [leetcode]3 Sum closest
问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST
▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...
- (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数
原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 3Sum Closest & 3Sum Smaller
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [leetcode]_K Sum 问题
问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...
随机推荐
- ROS(indigo)ABB机器人MoveIt例子
ROS(indigo)ABB机器人例子 参考网址: 1 http://wiki.ros.org/Industrial 2 http://wiki.ros.org/abb 3 https://gi ...
- Dynamics CRM 2013 Homepage Ribbon 按钮引用多个Javascript资源
在CRM的开发中ribbon的开发是比较重要的一环,很多客制化的功能都需要动用ribbon区,CRM2013中的名字已经改叫command bar了,但从老版本过来的人都还是习惯叫他ribbon. R ...
- Swift基础之闭包Closure学习
首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...
- Sqoop执行mysql删除语句
如果使用Sqoop删除mysql中的数据,并且传递动态日期参数,则使用下方的方法: 创建一个sh文件,内容如下: #!/bin/sh ## 环境变量生效 . /etc/profile #[调度删除导入 ...
- Java中Integer和String浅谈
Java中的基本数据类型有八种:int.char.boolean.byte.long.double.float.short.Java作为一种面向对象的编程语言,数据在Java中也是一种对象.我们用基本 ...
- python类:类方法和静态方法
http://blog.csdn.net/pipisorry/article/details/49516185 面相对象程序设计中,类方法和静态方法是经常用到的两个术语.逻辑上讲:类方法是只能由类名调 ...
- TCP的定时器系列 — SYNACK定时器
主要内容:SYNACK定时器的实现,TCP_DEFER_ACCPET选项的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上一篇博客中,已经连带 ...
- Chipmunk僵尸物理对象的出现和解决(四)
接上一篇,我们看看五角星和反弹棒碰撞时的代码: -(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair star:(CCNode * ...
- mysql进阶(四)mysql中select
mysql中select * for update 注: FOR UPDATE 仅适用于InnoDB,且必须在事务区块(BEGIN/COMMIT)中才能生效. 作用 锁定该语句所选择到的对象.防止在 ...
- 介绍几个好用的android自定义控件
首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionE ...