【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 - 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode] Multiply Strings 解题报告
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- [LeetCode] 3Sum Closest
依旧先来题目: Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- Leetcode Weekly 188 解题报告
文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...
- [LeetCode]3Sum Closest
题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...
- [LeetCode]Distinct Subsequences,解题报告
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Add Binary 解题报告
Add Binary Given two binary strings, return their sum (also a binary string). For example, a = " ...
最新文章
- 监控神器Prometheus用不对,也就是把新手村的剑
- 关于学习Python的一点学习总结(36->基本序列和映射协议)
- emqx速度_EMQX-3.0性能测试报告-ReadtheDocs.PDF
- Silverlight4 入门GetStart
- 无线AP如何区分来宾(流动)用户和正常用户?
- python客户端修改session_python中flask的Session设置的方法介绍
- dart系列之:数学什么的就是小意思,看我dart如何玩转它
- ubuntu16.04下安装NS-2.35以及对simple例的理解
- c语言爱心代码空心,c语言心形图案代码,是什么?
- 动态分区分配算法代码_【代码】巩敦卫等TEVC论文:基于区间相似度分析的协同动态区间多目标进化优化算法...
- 图片随意移动,可以拖动图片计算
- 批处理写的关机小程序--bat
- 自学elastic search
- copy-and-swap idiom详解和实现安全自我赋值
- php中in array函数_PHP函数in_array()使用详解
- 论述:数值计算中的精度问题
- 数据通信最新技术复习
- 传奇私服服务器移动玩家位置,传奇私服服务端里面哪个是玩家数据文件文件?...
- 【ELT.ZIP】《CCF开源高校行第一期》观后感
- 卷积层和全连接层的区别_CNN卷积层、全连接层的参数量、计算量
热门文章
- 卷积网络基础知识---Depthwise Convolution Pointwise Convolution Separable Convolution
- ASP .NET Core Web Razor Pages系列教程三:自动生成Razor Pages (CRUD)
- 鸿蒙智慧屏和pro有什么区别,体验揭秘荣耀智慧屏pro评测怎么样?荣耀智慧屏pro和普通版区别有什么不同?...
- java随机产生100个大小写字母_Java生成固定长度的随机字符串(以大小写字母和数字)...
- benchmarksql测试mysql_数据库压力测试工具 -- BenchmarkSQL 使用说明
- redis持久化 mysql_Redis 如何保持和MySQL数据一致
- linux php mysql安装完整版本_Linux下安装PHP+MySQL+Apache完整版
- plsql 设置鼠标行执行_Excel中执行“宏”的方法有哪些?我列举了这5个,你会几个...
- mysql default unix_timestamp_mysql中的unix_timestamp函数
- 框架和设计模式的区别