【LeetCode】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).
题解:
经过几个题的训练,基本上确定是两指针查找问题。找最接近target的三个数之和,第一个想法就是设两个差,一个为正,一个为负,在搜索过程中不断更新,最后比较两个差绝对值的大小,取绝对值小的差,与target相加即可。这里可以在循环内部跳过重复项,也可以不跳过(这样就会进行多余的若干次循环)。
Solution 1 (9ms)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int diffplus = INT_MAX, diffminus = INT_MIN+;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
int j = i + , k = n - ;
int a = nums[i];
while(j<k) {
int b = nums[j], c = nums[k];
if(a+b+c == target)
return target;
else if(a+b+c > target) {
diffplus = min(diffplus, a + b + c - target);
k--;
}
else {
diffminus = max(diffminus, a + b + c - target);
j++;
}
}
}
return abs(diffminus) < diffplus? target + diffminus : target + diffplus;
}
};
Solution 1 中我们用了两个变量存储差,那么能不能用一个呢,那么这个diff只能存储差的绝对值,我们怎么知道target该加还是减这个diff呢?解决办法就是在更新diff时同时更新result,在循环内时result == a+b+c;这样就无需target与diff的加减操作了,此时diff的作用只有一个:result是否更新的条件。
Solution 2 (9ms)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int result = nums[] + nums[] + nums[];
int diff = abs(result - target);
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
int j = i + , k = n - ;
while(j<k) {
int sum = nums[i] + nums[j] + nums[k];
int now_diff = abs(target - sum);
if(now_diff == ) return target;
if(now_diff < diff) {
diff = now_diff;
result = sum;
}
else if(sum > target) k--;
else j++;
}
}
return result;
}
};
【LeetCode】016 3Sum Closest的更多相关文章
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【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 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 【LeetCode】973. K Closest Points to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【LeetCode】259 3Sum Smaller
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- 【leetcode】973. K Closest Points to Origin
题目如下: We have a list of points on the plane. Find the Kclosest points to the origin (0, 0). (Here, ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
随机推荐
- IDEA------Error:java:无效的目标发行版:1/7
© 版权声明:本文为博主原创文章,转载请注明出处 使用IDEA发布java web项目时,报错.报错信息如下: 解决方案: 方案一:File-->Settings-->Build,Exec ...
- Spring MVC学习纲要
感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...
- android 在githup中的资源整理(转)
1.Github开源Android组件资源整理(一) 个性化控件(View) 2.Github开源Android组件资源整理(二)ActionBar和Menu 3. Github开源Android组件 ...
- 全能,OnSize的使用,部分覆盖后重画,都没有问题
import wx class View(wx.Panel): def __init__(self, parent): super(View, self).__init__(parent) self. ...
- centos安装php5.6
配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...
- 【Leetcode-easy】Roman to Integer
罗马数字转化为整数 * 1.基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: * 2.不能把基本数字 V .L .D ...
- 《CSS权威指南(第三版)》---第四章 值和单位
本章主要讲解的是一些属性声明用的值: CSS中的值主要有数字,百分数,颜色, 1.颜色: rgb(100%,100%,100%) OR rgb(255,255,255) OR #FF0000 WE ...
- Winfrom和控制台中static修饰方法的问题
在编写winform程序时,当写完方法名后,按Shift+Alt+F10(vs自动生成方法框架)后生成的方法是实例方法,而当手动为该方法添加static修饰符后,程序仍能正常运行. 而在控制台中,写完 ...
- 51nod 1537
题目 神犇题解 证明好巧妙,给跪OTZ 题目的式子:$ {\left( {1{\rm{ + }}\sqrt 2 } \right)^{\rm{n}}} $,设其乘开之后为 $ {\rm{a + b}} ...
- Contiki 源码风格
/** * \defgroup coding-style Coding style * * This is how a Doxygen module is documented - start wit ...