我现在在做一个叫《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. Python调用Google翻译

    出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...

  2. boot分区剩余空间不足

      Linux boot分区用于存放内核文件以及Linux一些启动配置文件,一般情况下分区大小为500M足够使用,如果出现空间不足的问题可以使用以下方法来解决. 查看已经安装的内核 dpkg --ge ...

  3. SignalR 设计理念(二)

    SignalR 设计理念(二) 实现客户端和服务器端的实时通讯. 前言: 客户端方法忽略大小写,主要原因基于是URL对大小写不敏感的问题,开发者之间为了更好的协同开发,定下的开发者协议. 问题阐述 客 ...

  4. NVIC配置中的分组详解

    在配置优先级的时候,要注意一个很重要的问题,中断种类的数量. NVIC只可以配置 16 种 中断向量的优先级,也就是说,抢占优先级和响应优先 级的数量由一个 4 位的数字来决定, 把这个 4 位数字的 ...

  5. 为控件动态添加Style

    此文可解决:  重写控件时,给控件加入子控件或父控件的样式切换问题. 很灵活的可以根据不同内容显示不同样式 子控件作用在: <DataTemplate x:Key="ColmunHea ...

  6. WinForm ListView不分页加载大量数据

    WinForm的ListView在加载大量数据时会出现闪烁的问题,同时数据加载很慢.如果你的列表中有超过千条的数据且不做特殊处理还是用普通的ListView.Items.Add(),估计你的用户得抱怨 ...

  7. Android Locale

    Locale 是用来适配语言和地区的.在实际使用过程中,如果使用不当还是会出现错误. 首先,需要了解的是,Locale 分两种,一种是语言,一种是地区.语言比如英语,地区比如美国.区别显而易见,说英语 ...

  8. NGINX部署配置参考.

    请求动态页面 1. uwsgi.ini配置文件.(主从负载uwsgi1.) 2. uwsgi2 的配置文件 3.查看. 4.结构图 5.配置 NGINX服务器  定义上游有哪些服务器. 定义转交给up ...

  9. iOS推送功能极光推送的介绍与实现

    1.个人整理操作流程 2.官方使用说明流程 2018iOS极光推送完整流程 极光推送官网

  10. 记一次生产发版时SpringBoot服务停用启用的问题

    近期项目交接,接手了个SpringBoot项目.生产环境里,jar包是通过软链接做成linux服务来启动和停用. 然而,每次通过jenkins构建发版,项目构建完毕,还要手动再去重启服务. 听交接的同 ...