题目
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).
分析
这题目与上一题十分相似。求出整数数组序列中与目标值最接近的三元组元素之和。
解这个题目条件反射的方法是三层遍历,求和比较,但是显而易见,在LeetCode提交时必然会出现超时的异常。所以必须另觅他法,这个题目的AC代码源于参考,在此表示感谢。
Time Limit Exceeded代码
class Solution {
public:
//方法一:三层穷举,超时异常
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
if (len < 3)
{
return 0;
}
int closest = nums[0] + nums[1] + nums[2];
for (int i = 1; i < len-2; i++)
{
//首先进行去重操作
if (nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < len - 1; j++)
{
if (nums[j] == nums[j - 1])
continue;
for (int k = j + 1; k < len; k++)
{
if (nums[k] == nums[k - 1])
continue;
int sum = nums[i] + nums[j] + nums[k];
if (sum == target)
{
closest = sum;
break;
}//if
else if (abs(sum - target) < abs(closest - target))
{
closest = sum;
}//elif
else
continue;
}//for
}//for
}//for
return closest;
}
int abs(int a)
{
return a > 0 ? a : 0-a;
}
};
AC代码
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
size_t size = nums.size();
if (size < 3)
{
cout << "num size must bigger than there!" << endl;
return 0;
}
sort(nums.begin(), nums.end()); // 对于以下的处理过程必须事先排序,类似二分搜索
int result = 0; // 记录最终结果
int distance = numeric_limits<int>::max(); // signed int
int sum = 0; // 中间结果
size_t i = 0, j = i + 1, k = size - 1;
for (i = 0; i < size - 2; i++) // 三元组的第一个元素一次遍历,范围为[0...n-3]
{
// 去重避免重复计算,如果和上次同则跳过
if (i > 0 && nums[i] == nums[i - 1])
{
continue;
}
j = i + 1; // 选定三元组第一个元素后,第二个元素从第一个元素的下一个位置开始考察
k = size - 1; // 选定三元组第一个元素后,第三个元素从数组末尾开始考察
while (j < k) // 三元组的后两个元素利用左右逼近来跳过效率,选定第一个元素后,其后的所有元素只需考察一遍
{
sum = nums[i] + nums[j] + nums[k];
if (sum == target) // 存在距离最近为0则直接返回,否则穷举选取非0最小距离
{
return sum;
}
else if (sum < target)
{
if ((target - sum) < distance)
{
result = sum;
distance = target - sum;
}
j = j + 1;
// 避免重复计算,如果和上次同则跳过
if (nums[j] == nums[j - 1])
{
j = j + 1;
}
}
else if (sum > target)
{
if ((sum - target) < distance)
{
result = sum;
distance = sum - target;
}
k = k - 1;
// 避免重复计算如果和上次同则跳过
if (nums[k] == nums[k + 1])
{
k = k - 1;
}
}
}
}
return result;
}
};
时间: 2024-10-27 05:34:54