LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度: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.
翻译:
给定一个n个整数的数组S,在S中找到三个整数,使三个数的和最接近目标数,返回三个整数的和。
您可以假设每个输入都有一个确定的答案。
思路:利用3sum里面的算法进行适当调整进行定位。
Code:125 / 125 test cases passed.——27ms(beats 20.85%) 时间复杂度:O(N2)
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int result = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i-1])
continue; // 相邻起点相同的情况已经在上一个点都判断过了
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right]; // 与3sumy一样
if (Math.abs(sum - target) < Math.abs(result - target)) {
result = sum;
}
// 找3sum时,是找确切的相等,所以找到后,三点的值就确定了,不可能有另一个不同的值可以对其中某一个进行替代(只能同时替换两个);
// 但是本题就算找到了也不能去重,因为三点不是确定的,有可能用一个点将三点中某一点进行替代形成更接近的数,所以不能用while去重【其实要去也行,需要加一个绝对值判断,见下面解释】
if (sum > target) {
right--;
} else {
left++;
}
}
}
return result;
}
不去重解释:
如果去重,假如num[left]与num[left+1]相同,消除了num[i]+num[left]+num[left+1]是‘’最接近的组合‘’可能
其实要去也不是没有办法,那就把这个可能在去重前进行判断记录即可:
if (left < right
&& nums[left] == nums[left + 1]
&& Math.abs(target - sum) < Math
.abs(target - result)) {
result = nums[i] + nums[left] * 2;
}
if (left < right
&& nums[right] == nums[right - 1]
&& Math.abs(target - sum) < Math
.abs(target - result)) {
result = nums[i] + nums[right] * 2;
}
明显,这种方法仅仅为了那一种情况增加算法复杂度是不明智的,在运行其他测试用例的时候都会慢一点,所以不采用。
参考答案:125 / 125 test cases passed.——26ms(beats 20.85%) 时间复杂度O(N2)
public int threeSumClosest(int[] num, int target) {
int result = num[0] + num[1] + num[num.length - 1];
Arrays.sort(num);
for (int i = 0; i < num.length - 2; i++) {
int start = i + 1, end = num.length - 1;
while (start < end) {
int sum = num[i] + num[start] + num[end];
if (sum > target) {
end--;
} else {
start++;
}
if (Math.abs(sum - target) < Math.abs(result - target)) {
result = sum;
}
}
}
return result;
}
和我的简直一毛一样
啊哈哈哈哈哈!

没想到,仅仅刷题几天,我已经如此强大了!简直百年难得的刷题奇才呀!
实验室路人甲:明明答案更快点。
朕:切,1ms能叫快嘛,我重新submit一下说不定还20ms了,略
实验室路人甲:那你看人家把nums[i]包括进sum了是不是比你少算几次?
朕:




好吧,确实应该把nums[i]包括进去。。。sum在此算法里应该灵活使用,即需要整体值时使用。
在讨论区还有大神将此题答案优化了一下(去重),好吧肯定不是我的去重那么复杂。。。。
Code:125 / 125 test cases passed.——22ms(beats 62.99%) 时间复杂度O(N2)
public int threeSumClosest3(int[] nums, int target) {
Arrays.sort(nums);
int sum = nums[0] + nums[1] + nums[nums.length - 1];
int closestSum = sum;
for(int i = 0; i < nums.length - 2; i++){
if(i==0 || nums[i]!=nums[i-1]){
int left = i + 1, right = nums.length - 1;
while(left < right){
sum = nums[left] + nums[right] + nums[i];
if(sum < target){
//move closer to target sum.已经确定sum比target小了,那么相同的left肯定也是小的
while(left<right && nums[left] == nums[left+1]){
left++;
}
left++;
}else if(sum > target){
//move closer to target sum.
while(left<right && nums[right] == nums[right-1]){
right--;
}
right--;
}else{
return sum;
}
//update the closest sum if needed.
if(Math.abs(target - sum) < Math.abs(target - closestSum)){
closestSum = sum;
}
}
}
}
return closestSum;
}
原来发现在【28行】找到更小值时不能进行去重,可以选择在【11-25行】指针调整的判断里面进行,并且增加了直接返回最优解,厉害厉害

LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium的更多相关文章
- 16 3Sum Closest(输出距离target最近的三个数的和Medium)
题目意思:给一个数组,给一个target,找三个数的和,这个和要与target距离最近,输出这个和 思路:这个题比3sum要稍微简单一点,如果需要优化,也可以去重,不过因为结果唯一,我没有去重. mi ...
- 【LeetCode每天一题】3Sum Closest(最接近的三数和)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- leetcode第16题--3Sum Closest
Problem:Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
随机推荐
- 第三课作业——set类型、sorted set类型的增删改查,redis的事务
第三课时作业 静哥 by 2016.2.23~2016.3.6 [作业描述] 1.总结什么是set以及什么是sorted set,并完成对set以及sorted set的增删改查(查需要至少4种方 ...
- https://www.cnblogs.com/yuanchenqi/articles/6755717.html
知识预览 一 进程与线程的概念 二 threading模块 三 multiprocessing模块 四 协程 五 IO模型 回到顶部 一 进程与线程的概念 1.1 进程 考虑一个场景:浏览器,网易云音 ...
- C#连接EXCEL和ACCESS字符串2003及2007版
97-2003版本 EXCEL Provider=Microsoft.Jet.OLEDB.4.0;Data Source=文件位置;ExtendedProperties=Excel 8.0;HDR=Y ...
- 《深入理解Linux网络技术内幕》阅读笔记 --- 路由基本概念
一.路由的基本概念 1.一条路由就是一组参数,这些参数存储了往一个给定目的地转发流量所需的信息,而一条路由所需的最少的参数集合为:(1)目的网络,(2)出口设备,(3)下一跳网关 2.路由中的相关术语 ...
- 我的Android进阶之旅------>Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
Android 设备的CPU类型通常称为ABIs 问题描述 解决方法 1解决之前的截图 2解决后的截图 3解决方法 4建议 为什么你需要重点关注so文件 App中可能出错的地方 其他地方也可能出错 使 ...
- Android View体系 系列文章
http://www.cnblogs.com/Free-Thinker/p/6768783.html
- [设计模式]迭代子模式 Iterator
迭代子模式又叫做游标cursor模式,是对象的行为模式.迭代子模式可以顺序的访问一个聚集中的元素而不必暴露聚集的内部表象. 迭代子模式被广泛的应用在Java语言的API中的几个设计模式之一.在Java ...
- Python(内置函数)
python英文官方文档详细说明:点击查看 lambda: map (加工,将各元素通过function加工后输出) map(function, iterable,...) reduce (综合,将后 ...
- Uber中国在地方城市的人员架构是怎样的?
http://www.thepaper.cn/newsDetail_forward_1390516 澎湃新闻:Uber中国在地方城市的人员架构是怎样的? 柳甄:一般是3人组成的小团队作战.一名城市 ...
- webservice -- cxf客户端调用axis2服务端
背景: 有个项目, 需要由第三方提供用户信息, 实现用户同步操作, 对方给提供webservice接口(axis2实现)并也使用axis2作主客户端调用我方提供的webservice接口 起初, 由于 ...