Array + two points leetcode.16 - 3Sum Closest
题面
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定数组,找出并返回最接近target的三个元素的和。可以假设,只有一个解。
样例
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
思路
我们在15题 3Sum中做过,三数加和为target的问题,采用了Two-Point逼近的方法。本题我们稍加改动就可以解决。
1. 数组排序,固定一个元素i,通过两点法去[i+1, size()-1]中搜索另外两个数字,先计算他们的和;
2. 若sum == target,直接返回;若不等,就需要判断sum与 我们给的初值res 那个更加接近target,即判断 abs(sum - target) 与 abs(res - target)的大小,对res进行更新,另外注意 l 和 r 的更新。
3. 返回res.
源码
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
if(len < )
return ;
//数组升序排序
sort(nums.begin(), nums.end());
int res = nums[]+nums[]+nums[];
for(int i=; i<len; i++)
{
if(i> && nums[i]==nums[i-])
i++;//避免i的重复,不必要
int l = i+, r = len-;
while(l < r)//两点法搜搜
{
int sum = nums[i] + nums[l] + nums[r];
if(sum == target)
return sum;
else if(sum > target)
{
if(abs(sum - target) < abs(res - target))
res = sum;
r--;
}
else
{
if(abs(sum - target) < abs(res - target))
res = sum;
l++;
}
}
}
return res;
}
};
Array + two points leetcode.16 - 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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 num ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [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 num ...
- 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 num ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [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 num ...
随机推荐
- linux简单命令4---压缩与解压
1:压缩命令:zip 2:.gz压缩,不能压缩目录,会压缩目录里的文件 3:.bz2压缩,不能压缩目录,直接报错 ------------------------------------------- ...
- SAP R3和SAP Business One的区别
SAP R3是SAP开发的 开发语言是ABAP. 之前叫SAP R/2 然后叫R/3 后又改叫ECC 现在叫A1了. 现在有新的版本S4 HANA : SAP发展史 SAP Business One是 ...
- navigationBarTitleText
想修改整个程序的导航栏,在app.json 文件 修改 "window": { "backgroundTextStyle": "light" ...
- [简短问答]LODOP如何查看用LODOP打印设计的代码
该博文为图文简短问答,具体详细介绍可查看本博客的相关博文,生成JS代码相关详细博文:Lodop打印设计(PRINT_DESIGN)介绍.Lodop打印设计.维护.预览.直接打印简单介绍.Lodop打印 ...
- LNMP V1.4正式版本安装及新增Let's Encrypt一键安装和其他功能
军哥的LNMP一键安装包已经有一些年头了,着实给需要在Linux VPS.服务器中安装WEB环境的用户提供不小的帮助,而且每年儿童节都会有较大版本的更新和升级.老左在二月份的时候有体验过LNMP V1 ...
- JS通过ActiveX读写ini配置文件
String.prototype.trim = function(){ return this.replace(/(^\s+)|(\s+$)/g, ''); }; IniConfig = functi ...
- 上传文件报错500或者文件大于2M上传不上去解决方法
修改php.ini 配置文件: 先找到配置文件------find / -name php.ini 打开php.ini修改内容:post_max_size ------ post请求上传参数的大小限制 ...
- [转载]桥接与NAT
NAT相当于是局域网中的局域网,把192.168.21.1当作外网ip,重新划分了一个网关(192.168.33.x) 网桥只是把网络桥接起来,还是原来的网关(192.168.21.x),虚拟机相当于 ...
- Java使用icepdf转高清图片
<dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</ar ...
- 前端通过将json转成excel文件下载
方法一: 将table标签,包括tr.td等对json数据进行拼接,将table输出到表格上实现,这种方法的弊端在于输出的是伪excel,虽说生成xls为后缀的文件,但文件形式上还是html,代码如下 ...