leetcode:3sum closet

题目:给一个数组和给定的目标值,要求在数组里找出三个元素,这三个元素的和最接近目标值,当然等于是最好的。

用3sum的方法,把判定条件作些修改。

int twoSum(vector<int> &num, int start, int target)
{
	if(num.size() < 3 || start >= num.size())
		return -target;
	int head = start;
	int tail = num.size() - 1;
	int result = -target;
	int erro = 0x7fffffff;
	while (head < tail)
	{
		int sum = num[head] + num[tail];
		if(sum == target)
			{
				result = target;
				return target;
		    }
		else if(sum < target){
			do
			{
				++head;
			} while (head < tail && num[head] == num[head - 1]);
		}
		else
		{
			do
			{
				--tail;
			} while (tail > head && num[tail] == num[tail + 1]);
		}
		int curerro = abs(sum - target);
		if(curerro < erro)
		{
			result = sum;
			erro = curerro;
		}
	}
	return result;
}
int threeSumClosest(vector<int> &num, int target) {
	const int len = num.size();
	if(len < 3)
		return 0;
	sort(num.begin(), num.end());
	int closet = -target;
	int erro = 0x7fffffff;
	for (int i = 0; i < len - 2; ++i)
	{
		int thisloop = twoSum(num, i + 1, target - num[i]);
		if(thisloop == target - num[i])
			return target;

		int cur_erro = abs(thisloop + num[i] - target);
	    if(cur_erro < erro)
		  {
			  closet = thisloop + num[i];
			  erro = cur_erro;
		}
		while(i + 1 < len && num[i + 1] == num[i])
 			++i;
	}
	return closet;
}

leetcode:3sum closet,布布扣,bubuko.com

时间: 2024-12-23 20:04:50

leetcode:3sum closet的相关文章

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

leecode -- 3sum Closet

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

LeetCode——3Sum &amp; 3Sum Closest

3Sum 题目 Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: ? Elements in a triplet (a, b, c) must be in non-descending order. (ie, a ≤ b ≤ c)

【leetcode】3Sum Closet

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

[LeetCode] 3Sum Closest

This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target. Also, some corner cases need to be handled; for example, nums does not have more than 2 elements. The code is as follows, which is quite

Leetcode——3Sum

原题如下: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

[LeetCode] 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