我现在在做一个叫《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. tomcat自动关闭了。

    测试方法: 1.狂点抽取大量数据的接口 结果: jvm里面的现成崩溃.导致tomcat错误. 思路: 最近发现tomcat老是自动关闭,开始也发现了,不过没放在心上,直到今天,请求一提交到服务器,to ...

  2. google-glog功能介绍

    google-glog功能介绍 分类: 其它类型2011-08-19 10:06 6618人阅读 评论(0) 收藏 举报 cookiesgooglestreammodulemapreducenull ...

  3. uwp ListView列表滑动特效

    在看过一篇文章 WPF自定义控件之列表滑动特效 PowerListBox  http://www.cnblogs.com/ShenNan/p/4993374.html#3619585 实现了滑动的特效 ...

  4. 《JavaScript高级程序设计》5.5 Function类型

    5.5 Function类型 函数实质上是对象, 每个函数都是Function类型的实例, 并且都和其他引用类型一样具有属性和方法. 因此函数名实际上也是一个指向函数对象的指针, 不会与某个函数绑定. ...

  5. 一文看尽 Raft 一致性协议的关键点

    本文由  网易云 发布. 作者:孙建良 Raft 协议的发布,对分布式行业是一大福音,虽然在核心协议上基本都是师继 Paxos 祖师爷(Lamport) 的精髓,基于多数派的协议.但是 Raft 一致 ...

  6. java简单的邮件发送

    java实现简单的邮件发送案例,学会了这个你就可以利用这个来整你的好友了,不断地给他进行邮箱轰炸(当然个人不建议瞎搞),最重要的是明白其中的原理最好了.话不多说,直接看代码案例了.首先需要导入的jar ...

  7. 【转】Bri's改装笔记

    网上关于三菱蓝瑟的改装方案的文章不少,但在以不换发动机为前提的理性改装确是这两篇和东南汽车俱乐部科仔的那篇<4G18的低成本NA玩法>最具参考价值. 小排量NA车的乐趣不在于跟人比直线加速 ...

  8. openvswitch BFD 简介

    为了保护关键应用,网络中会设计有一定的冗余备份链路,网络发生故障时就要求网络设备能够快速检测出故障并将流量切换至备份链路以加快网络收敛速度.目前有些链路(如POS)通过硬件检测机制来实现快速故障检测. ...

  9. 【UVA11107 训练指南】Life Forms【后缀数组】

    题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...

  10. Nginx + uWSGI 部署Django 项目,并实现负载均衡

    一.uWSGI服务器 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI ...