[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 ...
随机推荐
- JavaScript原型链demo
function Person(name){ this.name = name; } Person.prototype = { say: function(){ alert('hi'); }, say ...
- C#XML创建与节点对象引用
我们在创建xml过程中会遇到不同的级别有相同节点的情况.如下面的xml: <?xml version="1.0" encoding="GBK"> & ...
- 面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
package com.Amazon.interview; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.c ...
- 用Python和Django实现多用户博客系统(二)——UUBlog
这次又更新了一大部分功能,这次以app的形式来开发. 增加博客分类功能:博客关注.推荐功能(ajax实现) 增加二级频道功能 更多功能看截图及源码,现在还不完善,大家先将就着看.如果大家有哪些功能觉的 ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- 安装pip之后,执行pip,提示:unknown or unsupported command install
安装pip之后,在dos命令框中输入pip,提示:unknown or unsupported command install,搜索之后,立马在:http://stackoverflow.com/qu ...
- Navigation bar 的注意事项
Navigation bar 的注意事项 Bar button item 使用 button 作为 custom view,初始化 isEnabled 为 false,注意顺序 需要设置 bar bu ...
- iOS项目架构文档
设计的项目架构主要引用MVVM+MVC架构,并以功能模块分级.以下为目录结构. 初级目录: 我们只需要关注SGZH文件夹下的目录,其他为Xcode管理的目录.可以看到此目录为项目初级目录,我们开发过程 ...
- docker下使用caffe的命令记录
查看所有的images sudo docker images 利用某个image生成container sudo docker run -it --net=host -v /home/tingting ...
- webservice 的权限验证
1. http://blog.csdn.net/jaune161/article/details/25602655 2. http://wcp88888888.iteye.com/blog/13993 ...