[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 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).
问题:给定一个数组和一个整数 n ,求数组中的三个元素,他们的和离 n 最近。
这道题和 3Sum 很相似,解题思路也很相似,先排序,然后采用双指针法依次比较。
由于 3Sum 很相似,思路就不详说。主要说下不同点:
- 由于是求最接近值,当 s[i] + s[l] + s[r] - target == 0 的时候,即可返回结果。
- 返回的结果是最接近 n 的三个元素之和。
int threeSumClosest(vector<int>& nums, int target) {
std::sort(nums.begin(), nums.end());
int closest = target - nums[] - nums[] - nums[];
for (int i = ; i < nums.size(); i++) {
int l = i + ;
int r = (int)nums.size() - ;
int newTarget = target - nums[i];
while (l < r) {
if (nums[l] + nums[r] == newTarget) {
return target;
}
int diff = newTarget - nums[l] - nums[r];
if (abs(diff) < abs(closest)) {
closest = diff;
}
if (nums[l] + nums[r] < newTarget){
l++;
}else{
r--;
}
}
}
return target - closest;
}
[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, ...
- 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 JAVA
题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...
- [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 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 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 th ...
- 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 ...
随机推荐
- CodeFirst 的编程方式
第一步:创建控制台项目第二步:添加新建项目→Ado.Net空实体模型第三步:添加实体:Customer,添加几个必要的测试字段第四步:添加实体之间的联系第五步:根据模型生成数据库脚本,并执行sql脚本 ...
- 易买网(注册Ajax讲解)
关于注册(用到Ajax) 运用onblur进行时时刷新 创建所需用的Servlet 好了 Ajax其实不是很难 如果还是不懂可以私信我呦-^^-!
- 用javascript操作xml(二)JavaScript 将XML转换成字符串(xml to string)
function xmlToString(xmlData) { var xmlString; //IE if (window.ActiveXObject){ xmlString = xmlData.x ...
- 离开ACM了,总结一下
写这篇博客,一如当初我对着电脑显示器,不知道从哪里下手才是,所以没准写着写着就出现了倒叙插叙补叙等充满语文功底的修辞手法,不过不会有45度的妩媚和忧伤. 像一位程序员所说:今天的努力是为了儿时吹过的牛 ...
- asp.net mvc 发送邮箱验证码
public ActionResult Index() { /*第一种,利用Google的smtp来发送邮件*/ SmtpClient client = ); Random Rdm = new Ran ...
- 标签form表单里的属性简单介绍了一下
<form id="form1" name="form1" method="post" action=""> ...
- uva 1453 - Squares
旋转卡壳算法: 直接在这个上面粘的模板 主要用途:用于求凸包的直径.宽度,两个不相交凸包间的最大距离和最小距离··· 这题就是求凸包的直径 #include <cstdio> #inclu ...
- csu 10月 月赛 B 题 Scoop water
一个卡特兰数的应用: 卡特兰数主要有以下几个用途: 1.不同的出栈入栈数: 2.n个点组成的不同的二叉树的数目: 3.凸多边形的三角剖分划分: 4.括号化问题: 通项公式是:h(n) = C(2n-2 ...
- 转:Java学习路线图
作者: nuanyangyang 标 题: Java学习路线图(整理中,欢迎纠正) 发信站: 北邮人论坛 (Mon Aug 11 19:28:16 2014), 站内 [以下肯定是不完整的列表, ...
- 【网络流24题】No.21 (最长 k 可重区间集问题 最长不相交路径 最大费用流)
[] 输入文件示例input.txt4 21 76 87 109 13 输出文件示例output.txt15 [分析] 直接co题解好了,写得挺全.. [建模方法] 方法1 按左端点排序所有区间,把每 ...