【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 ...
随机推荐
- 在js中关于同名变量和函数的地位争夺问题
先上一段让大家比较蒙圈的代码,接下来再慢慢讲解 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 创建一个 Django 项目
一. 创建项目 其中: 确认项目是否创建成功: 在 manage.py 目录上运行 python manage.py runserver server 启动后,在浏览器访问 http://127.0. ...
- 12种CSS BUG解决方法与技巧
一. 针对浏览器的选择器 这些选择器在你需要针对某款浏览器进行css设计时将非常有用. IE6及其更低版本,本文由52CSS.com整理,转载请注明出处! * html {} IE7及其更低版本 *: ...
- 算法38---292. Nim游戏
1.题目: 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函 ...
- Linux 重启防火墙失败
CentOS 7 执行service iptables start出现redirecting to systemctl ...Failed to ...not loaded. 如果出现以下错误,好像说 ...
- SSIS安装以及安装好找不到商业智能各种坑
原文:SSIS安装以及安装好找不到商业智能各种坑 这两天为了安装SSIS,各种头疼.记录一下,分享给同样遇到坑的.. 安装SSIS需要几个步骤. 先说一下我的情况,安装SQL的时候,一直默认下一步,没 ...
- logstash-shipper.conf
input { file { path => '/data/rsyslog/*/*/*.log' start_position => 'beginning' sincedb_path =& ...
- ETL工具-informatica产品部分功能、接口采购梳理
在项目中,经常遇到要进行产品采购,虽然一直在使用informatica工具做数据的抽取.清晰转换.加载,但是使用的功能也比较初级.在遇到采购时大致的进行了梳理. 序号 名称 产品功能说明 产品选配说明 ...
- Redhat 6配置本地Yum源
注明:我的方法适用于iso镜像(光盘或光盘镜像:iso9660) 1.挂载(mount) 其它的mount方法可參见此链接 http://www.jb51.net/os/RedHat/1109.htm ...
- hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...