【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 ...
 
随机推荐
- Hibernate学习三----------session详解
			
© 版权声明:本文为博主原创文章,转载请注明出处 如何获取session对象 1. openSession 2. getCurrentSession - 如果使用getCurrentSession需要 ...
 - Anaconda装OpenCV
			
感谢来源: http://blog.csdn.net/fairylrt/article/details/43560525 前两天看到段子说开源软件就是各种配置,这是一件很辛苦的事情. Anacond ...
 - ServletContext读取配置文件
			
package servlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStrea ...
 - 关于React的Container&Presentational Component模型结构分析
			
react.js javascript 3 之前翻译了两篇关于Container&Presentational Component模型的文章,一篇是基础的Container和Component ...
 - 基于imgAreaSelect的用户图像截取
			
前言:想到用户资料中一般有个图像自我截取的部分,为什么要截取呢,因为好看了.so,经过我各种百度,各种参考,终于打工搞成了,写下纪念纪念,让以后拿来就用也好. 一:想前端ui这东西,我就懒得说话了,哎 ...
 - Android - 使用messager实现进程间通信(服务器端→客户端,客户端→服务器端双向)
			
之前看了一篇,然后不自己动手肯定是不行的,然后自己又写了一遍. 背景: 一般使用messenger进行进程间通信的时候,我们只能进行单方向通信.但是有没有办法让服务器端和客户端进行双向通信呢? 解决思 ...
 - python之Matplotlib 和Numpy
			
1.matplotlib http://www.cnblogs.com/TensorSense/p/6802280.html https://wenku.baidu.com/view/e1c15c9d ...
 - 用nvm管理windows nodejs时用npm全局安装的插件无法调用的解决方案
			
在环境变量中啊新建变量NODE_PATH赋值为prefix设置的地址即 prefix=D:\Users\xxx\AppData\Roaming\nodejs\npm-global 然后把%NODE_P ...
 - 基于EasyNVR二次开发实现自己的摄像机IPC/NVR无插件化直播解决方案
			
在之前的博客中<基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控>,我们已经比较多的描述EasyNVR所实现的功能,这些也在方案地址:http://www.eas ...
 - Swift 学习笔记 (闭包)
			
闭包是可以在你的代码中被传递和饮用的功能性独立模块.Swift中的闭包和C以及Objective-C中的Block很像,和其他语言中的匿名函数也很像. 闭包能捕获和存储定义在其上下文中的任何常量和变量 ...