题目:

  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取绝对值,每次找出误差最小值及对应的三个数的和。

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int len=nums.length;
int closestVal=Integer.MAX_VALUE;
int closestSum=nums[0]+nums[1]+nums[2];
Arrays.sort(nums);
int currentNum=nums[0];
for(int i=0;i<len-2;i++){
if(i>0 && nums[i]==currentNum) continue;
int begin=i+1,end=len-1;
while(begin<end){
int sum=nums[i]+nums[begin]+nums[end];
int absVal=Math.abs(sum-target);
if(absVal<closestVal){
closestVal=absVal;
closestSum=sum;
}else if(sum<target){
begin++;
}else end--;
}
currentNum=nums[i];
}
return closestSum;
}
}

  

【LeetCode】16. 3Sum Closest的更多相关文章

  1. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  2. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

  3. 【LeetCode】016 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  4. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  5. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  7. Leetcode Array 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. 【LeetCode】973. K Closest Points to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  9. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

随机推荐

  1. 154 Find Minimum in Rotated Sorted Array II

    多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...

  2. wordpress主题结构_源码

    WordPress博客主题的工作机制 WordPress主题由一系列模板文件组成,每个文件分别控制主题的特定区域.无论你处于哪个页面都能看到的网站的静态部分,由header文件.sidebar和foo ...

  3. Filter实现全站违法关键词屏蔽

    思路:客户端请求服务器数据,经过Filter过滤(请求放行,响应拦截),服务器向客户端返回数据时,在Filter中修改掉返回数据中违法的部分. 修改服务器的响应需要自定义一个HttpServletRe ...

  4. PLSQL_性能优化系列03_Oracle Parallel并发处理

    2014-09-25 Created By BaoXinjian

  5. SecureCRT 的安装和注册

    基本信息:win764位 准备: 下载软件包:scrt70-x86.exe,和SecureCRT.7.3.keygen.exe注册机 下载地址:http://download.csdn.net/det ...

  6. error-iis-Service Unavailable

    Service Unavailable Service Unavailable HTTP Error 503. The service is unavailable.

  7. IceGrid负载均衡部署 z

    [IceGrid负载均衡部署步骤]1.环境主机1:IP=192.168.0.239,上面部署注册表服务器registry和节点node1,registry和node1运行在同一进程中:主机2:IP=1 ...

  8. 证书 pki

    对称加密         symmetric cryptographic 非对称加密     asymmetric cryptographic 密钥交换协议 key agreement/exchang ...

  9. ubuntu vnc install

    windows & ubuntu http://www.jb51.net/os/Ubuntu/104948.html ubuntu & ubuntu https://www.digit ...

  10. ViewPager切换动画

    http://blog.csdn.net/lmj623565791/article/details/38026503 http://www.cnblogs.com/tianzhijiexian/p/4 ...