3Sum Closest
来源:互联网 发布:淘宝经典差评 编辑:程序博客网 时间:2024/06/10 12:16
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).
这道题是3sum的变形,是找出与给出的目标值最接近的那三个数的和
3sum那道题是变形成了两个数的和等于一个定值,然后首尾有两个指针,而这道题也可以变形成两个数的和最接近一个目标值,不过为了更方便理解,我们可以使用三个指针
引用他人的解析:
Sort the vector and then no need to run O(N^3) algorithm as each index has a direction to move.
The code starts from this formation.
----------------------------------------------------^ ^ ^| | || +- second third+-first
if nums[first] + nums[second] + nums[third] is smaller than the target, we know we have to increase the sum. so only choice is moving the second index forward.
----------------------------------------------------^ ^ ^| | || +- second third+-first
if the sum is bigger than the target, we know that we need to reduce thesum. so only choice is moving 'third' to backward. of course if thesum equals to target, we can immediately return the sum.
----------------------------------------------------^ ^ ^| | || +- second third+-first
when second and third cross, the round is done so start next round by moving 'first' and resettingsecond and third.
---------------------------------------------------- ^ ^ ^ | | | | +- second third +-first
while doing this, collect the closest sum of each stage by calculating and comparing delta. Compareabs(target-newSum) and abs(target-closest). At the end of the process the three indexes will eventually be gathered at the end of the array.
---------------------------------------------------- ^ ^ ^ | | `- third | +- second +-first
if no exactly matching sum has been found so far, the value in closest will be the answer.
代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums,int target) {
if(nums.size()<3)return 0;
std::sort(nums.begin(),nums.end());
int frist,second,back,sum;
int ret=nums[0]+nums[1]+nums[2];
for(int i=0;i<nums.size()-2;i++)
{
frist=i;
second=i+1;
back=nums.size()-1;
while(second<back)
{
sum=nums[frist]+nums[second]+nums[back];
if(abs(sum-target)<abs(ret-target))ret=sum;
if(sum<target)second++;
else if(sum>target)back--;
else return target;
}
}
return ret;
}
};
- 3 Sum & 3 Sum Closest
- 【Leetcode】3Sum Closest (Sum)
- LeetCode: 3 Sum Closest
- 3Sum Closest
- leetcode - 3 sum closest
- leetcode 3Sum Closest
- LeetCode: 3Sum Closest
- [Leetcode]3Sum Closest
- Leetcode: 3SUM Closest
- LeetCode 3Sum Closest
- [Leetcode] 3Sum Closest
- leetcode002:3sum closest
- LeetCode : 3Sum Closest
- LeetCode 3Sum Closest
- 3Sum Closest
- 3Sum Closest
- 3Sum Closest
- [LeetCode] 3Sum Closest
- 深入理解javascript中return的作用
- TCP的三次握手(建立连接)和四次挥手(关闭连接)
- 作为未来VR与移动设备交互新手段,“眼动追踪”怎样用上MEMS?
- GIT常用命令清单
- requests库中cookies用法
- 3Sum Closest
- Leetcode题解
- python redis
- 分布式发布订阅消息系统 Kafka 架构设计
- 51nod 判断线段是否相交 poj Segments直线与多条线段相交
- C++之对象的封装
- 关于recyclerview的点击无效和错位
- db与放大倍数之间的关系
- MFC利用halcon以及cimage在picture control控件上面显示图片