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).
这道题解法,排序,加两端搜索;
出现的问题,理解错误,想避免重复的数据再次计算,对重复的数跳过,但是这个出现了错误。原因是假设数组为
-20 -19 -19 -19 0 1 2 1 1 1111111111111
给定值为-59,那么实际最接近为-58 ;
当前值为-20, l为-19 h 为1,加入跳过重复值则数组变为
-20 -19 0, 1, 2 1 这样就出现错误了
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <vector>
using namespace std;
int partition(vector<int>& nums, int l, int h)
{
int com = nums[l];
if (l >= h)
return l;
while (l<h)
{
while (h>l&&nums[h] >= com)
{
h--;
}
if (h>l)
nums[l++] = nums[h];
while (l<h&&nums[l] <= com)
l++;
if (l<h)
nums[h--] = nums[l];
}
nums[l] = com;
return l;
}
void quickSort(vector<int>& nums, int l, int h)
{
if (l >= h)
return;
int par = partition(nums, l, h);
quickSort(nums, l, par - 1);
quickSort(nums, par + 1, h);
}
int threeSumClosest(vector<int>& nums, int target) {
int l = 0;
int h = nums.size();
vector<int> mynums = nums;
quickSort(mynums, l, h - 1);
int mean = abs(nums[0] + nums[1] + nums[2] - target);
int ret = nums[0] + nums[1] + nums[2];
while (l<h)
{
int value = mynums[l];
int j = l + 1;
int k = h - 1;
while (j<k)
{
//while(j+1<k-1&&mynums[j]==mynums[j+1])
// j++;
/// while(j+1<k-1&&mynums[k]==mynums[k-1])
// k--;
int temp = value + mynums[j] + mynums[k];
if (abs(temp - target) <= mean)
{
ret = temp;
mean = abs(temp - target);
}
if ((temp - target)<0)
{
j++;
}
else if ((temp - target)>0)
{
k--;
}
else
{
return temp;
}
}
l++;
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { 13, 2, 0, -14, -20, 19, 8, -5, -13, -3, 20, 15, 20, 5, 13, 14, -17, -7, 12, -6, 0, 20, -19, -1, -15, -2, 8, -2, -9, 13, 0, -3, -18, -9, -9, -19, 17, -14, -19, -4, -16, 2, 0, 9, 5, -7, -4, 20, 18, 9, 0, 12, -1, 10, -17, -11, 16, -13, -14, -3, 0, 2, -18, 2, 8, 20, -15, 3, -13, -12, -2, -19, 11, 11, -10, 1, 1, -10, -2, 12, 0, 17, -19, -7, 8, -19, -17, 5, -5, -10, 8, 0, -12, 4, 19, 2, 0, 12, 14, -9, 15, 7, 0, -16, -5, 16, -12, 0, 2, -16, 14, 18, 12, 13, 5, 0, 5, 6 };
int ret = threeSumClosest(nums, -59);
return 0;
}