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).
题意:
给定一个数组和一个目标值,返回其中3个数字的和且和值最接近目标值。
思路:
先把数组排序,把3个数字的问题转换为找2个数字问题,每次固定一个数值,那么target就变为了(target-固定的数字),找两个数字近似新target就简单多了,定义两个指针,指向首和尾,定义一个Min,表示与新target的差值,差值越小越接近,不断的向后移动首指针或者向前移动尾指针来调整近似度Min,直到轮寻完毕为止,也就是每个值都被固定过。
代码:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size() == 0){
return 0;
}
//排序
sort(nums.begin(), nums.end());
auto End = nums.end()-1;
//初始化Min,为了第一次可以更新
int Min = 2147483647;
int ret = 0;
//每次固定一个值,轮寻完毕为止,注意最小的为nums.end()-2,因为每次是3个值来判断。
for(auto iter = nums.begin(); iter != nums.end()-1; ++iter){
//第一个迭代器为首,第二个为尾
auto iter1 = iter + 1;
auto iter2 = nums.end()-1;
//减去固定值产生新的target
int my_target = target-*iter;
int tmp;
//首尾迭代器不重合
while(iter1 != iter2){
tmp = *iter1 + *iter2;
if(tmp < my_target){
//判断是否有更小的近似度Min
if(abs(my_target-tmp) < Min){
Min = abs(my_target-tmp);
ret = *iter1 + *iter2 + *iter;
}
//移动迭代器
++iter1;
}
else if(tmp > my_target){
if(abs(my_target-tmp) < Min){
Min = abs(my_target-tmp);
ret = *iter1 + *iter2 + *iter;
}
--iter2;
}else{
//如果相等说明找到了等于target的三个值
return target;
}
}
}
return ret;
}
};