Problem : 找到给定数组中a+b+c 最接近target的组合
 
类似求三个数之和为0的思路:每次更新求和的result值,利用函数Math.abs()判断绝对值最小的为result
 
参考代码:
package leetcode_50;

import java.util.Arrays;

/***
*
* @author pengfei_zheng
* 最接近target的三个数组合
*/
public class Solution16 {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);//排序操作
int result = nums[0]+nums[1]+nums[nums.length-1];
for(int i=0;i<nums.length-2;i++){//遍历
int start = i+1,end = nums.length-1;
while(start<end){//移动两端指针
int sum = nums[i]+nums[start]+nums[end];
if(sum>target)//移动end指针
end--;
else start++;//移动start指针
if(Math.abs(sum-target)<Math.abs(result-target))
result=sum;//更新result值
}
}
return result;
}
}

LeetCode 16 3Sum Closest (最接近target的3个数之和)的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. [leetcode]16. 3Sum Closest最接近的三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  3. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  4. leetcode 16. 3Sum Closest JAVA

    题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...

  5. 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 ...

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

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

  7. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 大爱HTML5 9款超炫HTML5最新动画源码

    我们分享过很多漂亮的HTML5动画,包括CSS3菜单.HTML5 Canvas动画等.今天我们精选了9款非常不错的超炫HTML5最新动画及其源码,一起来看看. 1.HTML5可爱的404页面动画 很逗 ...

  2. andoid-sdk 安装时出现 Stopping ADB server failed(code -1) 错

    出错原因: cmd在path路径找不到adb命令,是因为adb.exe文件存在于android-sdk安装目录platform-tools/子目录下,要将这个路径配置到环境变量里面. 解决方案: 按照 ...

  3. scanf_s,scanf安全版本

    %s,%c必须加sizeof(it)

  4. 通过Nginx反向代理,IIS和apache 共用80端口

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  5. 利用opencv作透明重叠人群密度热度图

    在作热度图的时候我们经常需要将热度图调整透明度后叠加在原图上达到更好的展示效果.比如检测人气密度的热度图: (来自sensetime) 一般作图的时候会第一时间想到matplotlib,因为可以很方便 ...

  6. actor mysql 持久化之 specified actor

    持久化到mysql,要求一次操作涉及到的多次读写的事务性.使用的 library 是 postgresql-async, akka 版本是 2.11. 1. 实现 per-user 逻辑,简单来讲,就 ...

  7. PHP 5.4 中的traits

    PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西. Traits (横向重用/多重继 ...

  8. 转载 IMP时数据库的IO性能监控,并提供IOPS的计算方法

     IMP时数据库的IO性能监控,并提供IOPS的计算方法 2011-07-15 17:36:10 分类: Linux [root@ntkdb oradata]# iostat -x 1 10     ...

  9. SpringBoot(一)-- 知识点介绍

    一.简介 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置.简单来说,它提供了一堆依赖打包,并 ...

  10. Kali linux 试用:dnsenum

    dnsenum的目的是尽可能收集一个域的信息,它能够通过谷歌或者字典件猜测可能存在的域名,以及对一个网段进行反向查询.它可以查询网站的主机地址信息.域名服务器.mx record(函件交换记录),在域 ...