【LeetCode】3Sum Closest 解题报告
【题目】
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解题报告 非常像,与之不同的是,不再是求三个数的和是不是为0,而是看三个数的和与target的差是否为最小,仅仅需记录当前最优解并不断更新其值就可。
【Java代码】O(n^2)
public class Solution {
public int threeSumClosest(int[] num, int target) {
if (num == null || num.length < 3) return 0; Arrays.sort(num); int ret = 0;
int closestDist = Integer.MAX_VALUE;
int len = num.length;
for (int i = 0; i < len-2; i++) {
if (i > 0 && num[i] == num[i-1]) continue; int l = i+1, r = len-1;
while (l < r) {
int sum = num[i] + num[l] + num[r];
if (sum < target) {
if (target-sum < closestDist) {
closestDist = target - sum;
ret = sum;
}
l++;
} else if (sum > target) {
if (sum-target < closestDist) {
closestDist = sum - target;
ret = sum;
}
r--;
} else { //when sum == target, return sum.
return sum;
}
}
} return ret;
}
}
easy出错的地方是。把 ret 初始值设为 Integer.MAX_VALUE。然后后面计算 closestDist = Math.abs(ret - target),这样会导致溢出!。
【LeetCode】3Sum Closest 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [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 num ...
- 【LeetCode】259. 3Sum Smaller 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- 浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
html_entity_decode():把html实体转换为字符. $str = "just atest & 'learn to use '"; echo html_en ...
- 添加ArcGIS数据
加载arcgis server的rest服务瓦片数据:ol.layer.Tile+ol.source.TileArcGISRest 加载arcgis online的在线瓦片数据:ol.layer.Ti ...
- python 面向对象 继承
什么是继承 继承表达的是一种”是“的关系,比如人是动物 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 继承是基于抽象的结 ...
- 【BZOJ 1083】 [SCOI2005]繁忙的都市
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 很明显的最小生成树了. 输出最后选的那条边就好了. [代码] #include <bits/stdc++.h> usin ...
- hibernate ID生成策略配置
1.Student.hbm.xml配置 <hibernate-mapping package="com.wxh.hibernate.model"> <class ...
- 巧妇能为少米之炊(1)——Android下小内存下的生存之道
常常听到身边用安卓的朋友抱怨手机卡顿,内存动不动就快没了.而Google声称在512M的内存下也能流畅执行Android 4.4.究竟它做了什么? 总结一下它主要做了四件事: 1.优化内核,使用Act ...
- 【Android UI】案例02 圆角边框、圆角背景的实现(shape)
本文主要分享圆角边框与圆角背景的实现方式.该方式的实现,须要了解shape的使用.该部分的具体介绍,请阅读博客http://blog.csdn.net/mahoking/article/details ...
- 5.IntellijIDEA常用快捷键总结
转自:https://blog.csdn.net/qq_17586821/article/details/52554731下面的这些常用快捷键需要在实际操作中不断地体会才能真正感受到它们的方便之处. ...
- The in operator
The operators we have seen so far are all special characters like + and *, but there are a few opera ...
- String methods
A method is similar to a function – it takes arguments and returns a value – but the syntax is diffe ...