最接近给定值的子数组和

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 
using namespace std;

	int partition(vector& nums, int l, int h)
	{
		int com = nums[l];
		if (l >= h)
			return l;
		while (ll&&nums[h] >= com)
			{
				h--;
			}
			if (h>l)
				nums[l++] = nums[h];
			while (l& 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& nums, int target) {

		int l = 0;
		int h = nums.size();

		vector 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 (l0)
				{
					k--;
				}
				else
				{
					return temp;
				}
			}
			l++;
		}
		return ret;

	}

int _tmain(int argc, _TCHAR* argv[])
{
	vector 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;
}







你可能感兴趣的:(编程练习)