《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫《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]的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 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 ...
- 【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 ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- 第一部分 Mysql的基础
一.登录: mysql -h localhost -u root -p #其中,-h表示后面跟着的是服务器主机地址,-u后面跟着的是用户名,-p表示密码# 本地测试: 账号: 二.也可以省略写成: m ...
- ES6 学习笔记之三 函数参数默认值
定义函数时为参数指定默认值的能力,是现代动态编程语言的标配.在ES6出现之前,JavaScript是没有这种能力的,框架为了实现参数默认值,用了很多技巧. ES6 的默认参数值功能,与其他语言的语法类 ...
- PYQT5实现文件目录浏览
def setBrowerPath(self): download_path = QtWidgets.QFileDialog.getExistingDirectory(self, "浏览&q ...
- 使用FluentScheduler实现定时任务管理
之前定时任务一直用的Windows服务,前段时间发现FluentScheduler这个框架,他跟Quarz.Net,Hangfire一样都是任务调度框架,但是相对使用而言我觉得FluentSchedu ...
- 11-使用EF操作数据库
本篇博客对应视频讲解 回顾 上一篇教程我们讲了XML与JSON的序列化问题,我们可以看到序列化实际上也是不同形式的转换,我们通常要以字节流的形式做中转.同时我们也可以看到,对于序列化这种常见的需求,我 ...
- .NET Entity Framework (with Oracle ODP.NET)
一.前言 1.Entity Framework是什么? Entity Framework是微软对ORM框架的实现.类似的实现也有其它方式,如DevExpress 的XPO(eXpress Persis ...
- Java中的split和join
Javascript中的用于字符串和数组之间转换的split和join函数使用起来非常方便,在Java中也有这两个函数,只不过join是在apache commons的lang库里实现的. impor ...
- FFMpeg的一些基础配置
一 . CMakeLists.txt文件的使用 1.添加头文件的相对路径 : include_directories(include(这里面就是文件的名字)) 2.设置ffmpeg的库的路径(v7a或 ...
- “全栈2019”Java多线程第二十七章:Lock获取lock/释放unlock锁
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Greedy- 621. Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...