题目:

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的更多相关文章

  1. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  2. 【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 ...

  3. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  4. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

  5. 【LeetCode】973. K Closest Points to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  6. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  7. 【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 ...

  8. 【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, ...

  9. 【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 ...

随机推荐

  1. 平滑处理 - cvSmooth() 函数实现

    前言 平滑是图像处理中很重要的操作,其主要用途是降噪.Open CV 提供了 5 种不同的平滑方法:简单模糊,简单无缩放变换的模糊,中值模糊,高斯模糊和双边滤波,它们都通过函数 cvSmooth 实现 ...

  2. Encoding::CompatibilityError: incompatible character encodings: GBK and UTF-8

    直接grunt serve读的css是.tmp/css/main.css 而这个文件不通过build生成出来是这样: /* Encoding::CompatibilityError: incompat ...

  3. myql 5.6 安装

    环境: centos 6.5  192.168.9.28  4核4G 虚拟机 一. 安装编译源码所需要的工具和库 [root@localhost ~]# yum -y install gcc gcc- ...

  4. UVa 11586 - Train Tracks

    题目:给你一些积木碎片,每一个碎片的两端仅仅能是凸或凹(M或F).凸凹可拼起来.是否能拼成一个环. 分析:图论.欧拉回路.推断入度等于出度就可以,即M和F同样且大于1组. 说明:╮(╯▽╰)╭. #i ...

  5. 【Xcode学C-4】进制知识、位运算符、变量存储细节以及指针的知识点介绍

    一.进制知识 (1)默认是十进制.八进制前面加0.即int num1=015;是13.十六进制前面加0x/0X.即int num1=0xd.结果是13.二进制前面是0b/0B,即int num1=0b ...

  6. IOS UITableView reload 刷新某一个cell 或 section

    通常刷新整个列表 我们都使用[self.tableView reloadData]; 有的时候,有变化更新的只是某一行 row 或者是某个section 所以只更新这一行就好了 //一个section ...

  7. 51Nod 1225 余数之和 —— 分区枚举

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 1225 余数之和  基准时间限制:1 秒 空间限制:1 ...

  8. UVA11330 Andy's Shoes —— 置换分解

    题目链接:https://vjudge.net/problem/UVA-11330 题意: 给出n双鞋子,鞋子按左右左右地摆放,但“左右”是否为一对鞋子是不确定的.问:至少交换多少次鞋子,才能把每双鞋 ...

  9. WCF异常处理

    [读书笔记] 在进行分布式应用的异常处理时需要解决和考虑的基本要素: 异常的封装:服务端抛出的异常如何序列化传递到客户端 敏感信息的屏蔽:抛出的异常往往包含一些敏感的信息,直接将服务操作执行过程抛出的 ...

  10. shell将字符串转换为大写变量并将小写作为变量值

    group_name='a b c d e f g' for a in $group_name; do typeset -u a ; echo "$a='$(echo $a | tr '[A ...