题目:

  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. bzoj4702: 装箱游戏

    Description Alice和Bob正在玩一个非常无聊的游戏以打发时间.游戏是这样的.初始的时候,有n个箱子和m个物品.箱子.物 品都是不同的.因此有nm种方法把这些物品放到箱子里.两个人轮流操 ...

  2. android学习笔记20——ProgressDialog进度条对话框

    ProgressDialog==>进度条对话框 ProgressDialog本身就代表一个进度条对话框,程序只需要创建ProgressDialog实例,并将其显示出来就是一个进度条对话框:开发者 ...

  3. GL_Oracle Erp月结和年节流程讨论(概念)

    2014-02-06 Created By BaoXinjian

  4. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  5. HTML5 编辑 API 之 Range 对象(一)

     一.Range 对象基本概念 通过使用 Range 对象所提供的方法实现一个鼠标选取内容,通过点击按钮打印出选中内容,当然注意在不同的浏览器下可选中的内容数量是不同的. <!DOCTYPE h ...

  6. studio--如何将Eclipse中的项目迁移到Android Studio 中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...

  7. GDI+ 中发生一般性错误。

    GDI+ 中发生一般性错误. “/wechat”应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及 ...

  8. 利用 Gitbook 生成文档中心站点

    经过一个多月,Bugtags 最近上线了自己的文档站点:docs.bugtags.com,在这里你可以找到 Bugtags 集成.使用相关的绝大部分问题. 在这之前我们使用的是第三方提供的帮助中心产品 ...

  9. Unable to save settings: Failed to save settings. Please restart IntelliJ IDEA 解决方案

    ubuntu 上 今天安idea 社区版,报这个错, 解决办法. 1 首先删除掉配置目录,例如安装的是idea最新的15.1版本,就删除 .ideaIC这个文件夹,重启解决了 2 而后,不大一会.又开 ...

  10. java程序内存使用

    一.内存使用示意图 二.java运行时数据区域 1.程序计数器: 当前线程所执行字节码的行号提示器. 2.java虚拟机栈: 线程私有,与线程生命周期相同,保存基本数据类型,如果线程所请求的栈深度大于 ...