No.016: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).
官方难度:
Medium
翻译:
给定一个长度为n的无序数组S,找出其中的3个整数,使这3个整数的和最接近于一个给定的目标值,返回这3个整数的和。
假定整个数组中的解是唯一的。
例子:
数组S:{ -1,2,1,-4},目标值target=1。
解为2(-1+2+1=2)。
- 这题的解法,与No.015(3Sum)的解法基本相同,先排序,然后通过外循环遍历+内循环夹逼的方法实现。
- 与3Sum问题不同的是,除了维护上一个值是否与当前值相同,其余的优化方法都不能使用。另外,previous的初始值,不能影响第一次计算。
- 维护一个差值closest,这个值要保证是正数。由于解唯一确定,所以在循环过程中,如果出现closest=0的情况,可以直接返回。注意返回值不是差值,而是三个数值的和。
- 注意入参检查。
解题代码:
// 返回值是数组3个元素的和,不是差值
public static int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
throw new IllegalArgumentException("Input error");
}
Arrays.sort(nums);
// 初始差值和返回值
int returnNo = Integer.MAX_VALUE;
int closest = Integer.MAX_VALUE;
int previous = Integer.MAX_VALUE;
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] == previous) {
continue;
}else{
previous = nums[i];
}
// 数组剩余部分夹逼
int left = i + 1;
int right = nums.length - 1;
// 转化为2Sum Closest问题
int remainTarget = target - nums[i];
int sum;
int preLeft = Integer.MIN_VALUE, preRight = Integer.MIN_VALUE;
while (left < right) {
if (nums[left] == preLeft) {
left++;
continue;
}
if (nums[right] == preRight) {
right--;
continue;
}
sum = nums[left] + nums[right];
// 解唯一确定,直接返回
if (remainTarget - sum == 0) {
return sum + nums[i];
}
// 最小值替换,返回值赋值
int temp = Math.abs(remainTarget - sum);
if (temp < closest) {
returnNo = nums[i] + nums[left] + nums[right];
closest = temp;
}
if (remainTarget - sum > 0) {
left++;
} else {
right--;
}
}
}
return returnNo;
}
threeSumClosest
相关链接:
https://leetcode.com/problems/3sum-closest/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.016:3Sum Closest的更多相关文章
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode OJ: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笔记:3Sum Closest
一.题目描写叙述 二.解题技巧 该题与3Sum的要求类似.不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路类似,先是进行排序.然后顺序选择数组A中的 ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- [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 ...
随机推荐
- ASP.NET MVC的Action拦截器(过滤器)ActionFilter
有时项目要进行客户端请求(action)进行拦截(过滤)验证等业务,可以使用拦截器进行实现,所谓的action拦截器也没有什么的,只是写一个类,继承另一个类(System.Web.Mvc.Filter ...
- 使用System.Drawing.Imaging.dll进行图片的合并
在最近开发项目的时候有时候需要进行图片的合并,即将两张图片合并成功一张图片 合并图片的代码: #region 两张图片的合并 /// <summary > /// 将Image对象转化成二 ...
- java中string内存的相关知识点
(一):区别java内存中堆和栈: 1.栈:数据可以共享,存放基本数据类型和对象的引用,其中对象存放在堆中,对象的引用存放在栈中: 当在一段代码块定义一个变量时,就在栈中 为这个变量分配内存空间,当该 ...
- (转)Shell函数
Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1. 函数 ...
- 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理
在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能给系统用户更好的操作体验,也提高了用户录入数据的效率.我在较早时期的EasyUI的Web框架上, ...
- 献给所有从事IT行业拥有梦想的英语渣们
本文适合读者:从小到大英语都不好,如今选择IT行业需要重拾英语追寻梦想的人,英语大神们请绕行.. 目录 一.背景介绍 二.明确学习目标 2.1 英文文法 2.2 词汇量 三.题外话 3.1 关于本文 ...
- 老鸟谈谈JAVA EE的学习
老鸟谈谈JAVA EE的学习 因为出差和项目的原因,有将近一个月的时间没有更新博客了,今天终于得闲,和兄弟们分享一下JAVA EE的学习心得.书中带过,直入主题,下面我们首先看看什么是JAVA EE. ...
- Windows Azure Cloud Service (41) 修改云服务IIS托管管道模式为4.0经典模式
<Windows Azure Platform 系列文章目录> 这是笔者在之前的项目中遇到的问题,做一下总结,给网友做参考. 在一般情况下,Visual Studio开发的Cloud Se ...
- 【数据压缩】Huffman编码
1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...
- 研究 研究而已 java和.net的HashSet对比
各位看官,,我不是在引发战争,,我只是想知道事情的真想,我源之于,一段代码我需要实现C#和java的两个版本功能,才发现这一个对比.讨论问题的实质,为什么会出现这样的问题.java和C#都很优秀.请大 ...