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. Php反转字符串函数

    From: http://blog.csdn.net/happy664618843/article/details/5861138

  2. 令人惊叹的HTML5动画及源码分析下载

    HTML5非常酷,利用HTML5制作动画简直让我们忘记了这世界上还有flash的存在.今天我们要分享的一些HTML5动画都还不错,有些动画设计还是挺别出心裁的.另外,每一款HTML5动画都提供源代码下 ...

  3. phd文献阅读日志-博一下学期

    博一下学期: 1.week1,2018.2.26 2006-Extreme learning machine: theory and applications 期刊来源:Huang G B, Zhu ...

  4. selenium 如何处理table

    qi_ling2005  http://jarvi.iteye.com/blog/1477837 andyguo  http://blog.csdn.net/gzh0222/article/detai ...

  5. Java编程思想学习笔记——类型信息

    前言 运行时类型信息(RTTI:Runtime Type Information)使得我们可以在程序运行时发现和使用类型信息. Java在运行时识别对象和类的信息的方式: (1)一种是RTTI,它假定 ...

  6. Java实习生面试题整理

    一.数据类型 包装类型 八个基本类型: boolean/1 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 基本类型都有对应的包装类 ...

  7. Centos7以上的版本 mysql 无法启动,无法停止问题

    service mysqld start 始终提示如下: Failed to issue method call: Unit mysqld.service failed to load: No suc ...

  8. Sql server连接数据库报错相关

    情况一:此版本的 SQL Server 不支持用户实例登录标志. 解决方法: 方法1:在连接属性的设置里边,点高级,将User Instance 设置为false,默认的true(我在中没有找到相应的 ...

  9. swagger的说明、配置及使用

    一.What is swagger? 官方介绍:Swagger是一个规范且完整的框架,提供描述.生产.消费和可视化RESTful Web Service.专业角度:Swagger是由庞大工具集合支撑的 ...

  10. ios的AutoresizingMask【转】

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum {   UIViewAutoresi ...