LeetCode--No.016 3Sum Closest
16. 3Sum Closest
- Total Accepted: 86565
- Total Submissions: 291260
- Difficulty: Medium
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的解法有点类似,我们可以先将数组排序,然后将数组中的数从左到右依次确定为第一个数,在其右边的数中寻找最接近的target的数即可。重要的是判断逻辑的思路。代码如下:
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int closest = nums[0]+nums[1]+nums[2] ;
for(int i = 0 ; i < nums.length-2 ; i++){
int l = i+1 ;
int r = nums.length-1 ;
while(l < r){
int sum = nums[i] + nums[l] + nums[r] ;
if(sum == target){
return sum ;
}else if(sum < target){
/*
* 三种情况:
* 1、closest < sum < target --->closest = sum
* 2、sum < target < closest --->| target-sum < closest-target ---> closest = sum
* | target-sum >= closest-target --> closest不变
* 3、sum <= closest <= target ---> closest不变
*/
if(sum > closest){
//情况1
closest = sum ;
}else if(closest > target){
//情况2
if(target-sum < closest-target){
//情况2.1,
closest = sum ;
}
//情况2.2不用变化
}
//情况3不同变化
l++ ;
}else{
//分析同上
if(sum < closest){
closest = sum ;
}else if(closest < target){
if(sum-target <= target-closest){
closest = sum ;
}
}
r-- ;
}
}
}
return closest ;
}
LeetCode--No.016 3Sum Closest的更多相关文章
- 【LeetCode】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- 【JAVA、C++】LeetCode 016 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]016. 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...
- 【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 ...
随机推荐
- 数字三角形/数塔问题(DP入门题)
有形如下图所示的数塔,从顶部出发,在每一结点可以选择向左走或是向右走,一起走到底层,要求找出一条路径,使路径上的值最大. 样例输入: 5 13 11 8 12 7 26 6 14 15 8 12 7 ...
- Windows导入EOS工程
[Windows导入EOS工程] 编写 EOS 智能合约时,有许多EOS提供的结构.函数.宏.文档对这些内容的描述毕竟模糊,再多的文档也比不过看实际的代码,所以还要下载EOS代码看一下. 最好有个ID ...
- “AS3.0高级动画编程”学习:第三章等角投影(上)
什么是等角投影(isometric)? 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com 刚接触这个概念时,我也很茫然,百度+google了N天后,找到了一些文章: [转 ...
- 从汇编层面解释switch语句判断快速的原因
源码如下: #include <stdio.h> void main(){ int flag; flag=1; switch (flag){ ...
- webapi postman 415 错误
https://blog.csdn.net/Intangible_moon/article/details/80183121 猜测:如果后台接口使用的是[fromBody]标签的话,需要使用raw方式
- 单系统登录机制SSO
一.单系统登录机制 1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务器会独立处理,不与之前或之后的请求产生关 ...
- RNA提取和建库流程对mRNA-Seq的影响
RNA提取和建库流程对mRNA-Seq的影响 已有 10460 次阅读 2014-8-14 14:21 |个人分类:转录组测序|系统分类:科研笔记|关键词:转录组测序,RNA-Seq,,链特异性RNA ...
- springmvc为什么是线程不安全的
1.因为springmvc默认是单例的,所以会有线程安全的问题,如果存在全局变量,因为全局变量是存在方法区的,而局部变量是放在栈中的,方法区是所有线程公用的,而每个线程都有属于自己的栈.所以如果使用单 ...
- ABP框架系列之三十八:(NHibernate-Integration-NHibernate-集成)
ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with NHibernate. T ...
- 隐藏字符 BOM
如果您在修改任何PHP文件後发生: * 不能登入或者不能登出: * 页顶出现一条空白: * 页顶出现错误警告: * 其它不正常的情况. 则多半是编辑器的问题. 本程序采用UTF-8编码.现在几乎所 ...