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

16. 3Sum Closest [M]

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

思路

这个问题等于是3SUM又升级了,因为这里现在是最接近的结果,所以Hashmap的思路基本没用了,因为必须得要循环找到所有可能。当然,这个题目有个限制就是只有唯一的结果,所以,如果发现完全相等的,也就可以停下来了。

但是解题思路还是可以参考3SUM,算法基本一样,有些地方需要修改一下。首先,因为是找最接近的,所以要考虑所有情况,left循环到length-2

    for (int left = 0; left < nums.length-2; left++)

然后还是mid和right分别从两端往中央扫描,如果mid+right还比较小,那就需要mid右移,反之right左移(每次如果有最小的就存下来)

我们可以写出如下的代码:

 mid = left+1; right = nums.length-1;
while(mid < right)
{
int tmp = target-nums[left];
if(abs(tmp - nums[mid] + nums[right]) < abs(target-Min))
Min = nums[left] + nums[mid] + nums[right]);
if(nums[mid] + nums[right] == tmp)
return Min;
else if(nums[mid] + nums[right] < tmp)
mid++;
else
right--;
}

代码

public class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int mid,right;
if(nums.length < 3)
return 0;
int Min = nums[0]+nums[1]+nums[2];
//left要循环全部
for (int left = 0; left < nums.length-2; left++) {
mid = left+1; right = nums.length-1;
int tmp = target-nums[left];
while(mid < right)
{
if(Math.abs(tmp - nums[mid] - nums[right]) <Math.abs(target - Min)) //每次查看是不是最小的情况
Min = nums[left]+ nums[mid] + nums[right];
if(nums[mid] + nums[right] == tmp)
{
return target; //因为只有一种答案所以可以直接返回
}
else if(nums[mid] + nums[right] < tmp)
mid++;
else
right--;
}
}
return Min;
}
}

《LeetBook》leetcode题解(16):3Sum Closest [M]的更多相关文章

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

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

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

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

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

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

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

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

  6. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

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

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

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

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

  9. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

随机推荐

  1. nginx 配置图片服务器 (window版本)

    配置nginx二级域名 ①找到配置文件 例如:%nginx_home%/conf/nginx.conf ②配置 #user nobody; worker_processes 1; #error_log ...

  2. Hive 1.2.1&Spark&Sqoop安装指南

    目录 目录 1 1. 前言 1 2. 约定 2 3. 服务端口 2 4. 安装MySQL 2 4.1. 安装MySQL 2 4.2. 创建Hive元数据库 4 5. 安装步骤 5 5.1. 下载Hiv ...

  3. Ubuntu16.04安装PostgreSQL并使用pgadmin3管理数据库_图文详解

    版权声明:本文地址http://blog.csdn.net/caib1109/article/details/51582663 欢迎非商业目的的转载, 作者保留一切权利 apt安装postgresql ...

  4. 20145234黄斐《java程序设计》第六周

    教材学习内容总结 第十章:输入与输出 InputStream与OutputStream 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而言的 InputStream与Output ...

  5. 对SpringDAO层支持的总结

    1.问题 1.JDBC/ORM框架(如Hibernate)开发中编程模型有哪些缺点?  如JDBC   2.解决方案(模板设计模式,本质:将可变的和不可变的分离) 模板方法模式:定义操作的步骤(固定的 ...

  6. Spring MVC深入讲解

    一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...

  7. Oracle SQL Trace 和 10046 事件

    http://blog.csdn.net/tianlesoftware/article/details/5857023 一. SQL_TRACE 当SQL语句出现性能问题时,我们可以用SQL_TRAC ...

  8. spring 注解实例

    先不说网上的那些例子了,百度到的都是一些零碎的东西.我之所以记博客,除了总结之外,很大一个原因是对网上的某些东西真的很无语. 拿注解来说,什么入门实例的东西,说是入门,却连一个基本的hello wor ...

  9. 你好,Azure DevOps Server 2019;再见,Team Foundation Server

    微软正式发布Azure DevOps Server 2019的第一个版本,作为Team Foundation Server (TFS)2018的升级版本和替代产品. 这是目前市面上唯一一款将产品名称冠 ...

  10. C#图片处理,缩略图制作

    准备参数:图片文件流.文件名 方法:1.先将图片流通过System.Drawing.Image.FromStream方法转成图片对象 2.通过图片对象.GetThumbnailImage方法生成自定义 ...