LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度: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 标签:Array的更多相关文章
- 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第[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每天一题】3Sum Closest(最接近的三数和)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)
题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...
- 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 ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
随机推荐
- hdu5418--Victor and World(floyd+状压dp)
题目链接:点击打开链接 题目大意:有n个城市.在n个城市之间有m条双向路.每条路有一个距离.如今问从1号城市去游览其他的2到n号城市最后回到1号城市的最短路径(保证1能够直接或间接到达2到n).(n& ...
- java多线程编程核心技术——第七章补漏拾遗
本章主要知识点: 1)线程组的使用 2)如何切换线程状态 3)SimpleDateFormat类与多线程的解决方法 4)如何处理线程异常. 这本书基本来到了终点,其实在第四章来说,核心(基础)的线程知 ...
- 小白的Python之路 day3 函数式编程,高阶函数
函数式编程介绍 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- Tableau Desktop 10.4.2 的安装和激活
在安装之前,首先我们要弄清楚Tableau是个什么鬼东西,我们为什么需要安装这款软件? Tableau将数据运算与美观的图表完美地嫁接在一起.它的程序很容易上手,各公司可以用它将大量数据拖放到数字&q ...
- 如何安装mysql
如何安装mysql对于初学者来说的确是很麻烦,首先要知道安装mysql仅仅只是安装一个mysql系统,是没有任何可视化操作界面的,所以还要安装一个mysql的管理工具,这是初学者容易蒙的地方之一. m ...
- js文件加载优化
在js引擎部分,我们可以了解到,当渲染引擎解析到script标签时,会将控制权给JS引擎,如果script加载的是外部资源,则需要等待下载完后才能执行. 所以,在这里,我们可以对其进行很多优化工作. ...
- node基础篇二:模块、路由、全局变量课堂(持续)
今天继续更新node基础篇,今天主要内容是模块.路由和全局变量. 模块这个概念,在很多语言中都有,现在模块开发已经成为了一种潮流,它能够帮助我们节省很多的时间,当然咱们的node自然也不能缺少,看下例 ...
- github emoji 表情列表
最新emoji大全:emoji列表 emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie:
- 豹哥嵌入式讲堂:ARM开发之文件详解(2)- linker文件
大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的linker文件. 在前一节课source文件详解里,豹哥给大家系统地介绍了source文件,source文件是嵌入式工程里典 ...