【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 ...
随机推荐
- hadoop生态系统学习之路(六)hive的简单使用
一.hive的基本概念与原理 Hive是基于Hadoop之上的数据仓库,能够存储.查询和分析存储在 Hadoop 中的大规模数据. Hive 定义了简单的类 SQL 查询语言,称为 HQL.它同意熟悉 ...
- 【BLE】CC2541之自己定义按键
本篇博文最后改动时间:2017年01月06日,11:06. 一.简单介绍 本文以SimpleBLEPeripheral为例.介绍怎样将普通IO口(P12)自己定义为按键. 注:本文加入按键方法不与协议 ...
- 在Mac OS X中下载Android源代码的一些经验
首先说明.随着最近(2014年6月開始)GFW的升级.这个站点:http://www.android.com/ 已经不能正常訪问了,以下的这些操作均是在我连接VPN的时候进行的. 首先,须要做一些准备 ...
- 浅谈iOS中MVVM的架构设计与团队协作【转载】
今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- HDFS源码分析之编辑日志编辑相关双缓冲区EditsDoubleBuffer
EditsDoubleBuffer是为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可以被flush.为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可 ...
- 小tip: DOM appendHTML实现及insertAdjacentHTML
一.无人不识君 据说今天是邓丽君奶奶会见马克思的日子,所谓“无人不识君”就多了份“无人不识邓丽君”之意. JS中有很多基本DOM方法,例如createElement, parentNode等,其中,a ...
- EasyNVR无插件IPC摄像机直播方案前端构建之:如何区分PC端和移动端
EasyNVR前端为了更好的用户体验,不仅仅设有PC客户端,还适应移动客户端: EasyNVR的客户端中PC端和移动端差异有很多.例如: 由于PC端.移动端自身硬件的差异,所需要展示的样式也会存在一定 ...
- android菜鸟学习笔记10----Intent及<intent-filter>
关于Bundle: 注意到Activity的onCreate()方法的签名是protected void onCreate(Bundle savedInstanceState),其参数是一个Bundl ...
- JavaScript演示如何访问Search字段
<!DOCTYPE html> <html> <body> <h3>演示如何访问Search字段</h3> <input type=& ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...