LeetCode之3SumClosest

题目:

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).

分析:题目大意是给定一个整型数组,要求求出其中三个数相加的结果与给定的目标值最接近。返回这个值。  还是和上一题差不多,重要的在于定界。

代码:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
	int threeSumClosest(vector<int>& num, int target) {
		int min_gap = INT_MAX;
		int result = 0;
		sort(num.begin(),num.end());
		auto last = num.end();
		int sum,gap;
		for(auto i = num.begin();i < last-2;++i){
			auto j = next(i);
			auto k = prev(last);
			while(j<k){
				sum = *i + *j + *k;
				gap = abs(sum - target); //取绝对值
				if (min_gap > gap){
					min_gap = gap;
					result = sum;
				}

				if (sum > target)
					--k;
				else
					++j;
			}
		}
        return result;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> v1;
	int rtn;
	v1.push_back(-1);
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(-4);

	Solution s;
	rtn = s.threeSumClosest(v1, 1);
	cout << rtn << endl;

	system("pause");
	return 0;
}
时间: 2024-07-29 12:32:01

LeetCode之3SumClosest的相关文章

leetcode 日记 3sumclosest java

整体思路为将threeSum将为twoSum即可 public int solution(int[] nums, int target) { if (nums.length == 3) { return nums[0] + nums[1] + nums[2]; } else { Arrays.sort(nums); int r = 10000;//此两处10000为省事而设,如果严谨应该大致找到其中的一个较大距离 int distance = 10000; for (int i = 0; i <

leetcode: 2Sum/3Sum/3SumClosest/4Sum系列问题(转载)

转载:http://blog.csdn.net/li4951/article/details/8693212 leetcode上有好几道关于数组中几个数据和为target的题目.恰好正在看剑指offer中"和为s的两个数组这章",据此思想,leetcode上的三道题目都被我解决了.总结一下. 1.twoSum: 输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s. 既然题目中已经提到了"递增数组",那么肯定不会暴力了.因此肯定有<O(n*n)

LeetCode: 3SumClosest

Title : 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

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

[LeetCode][JavaScript]3Sum Closest

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 arr

[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 Closest

题目链接:https://leetcode.com/problems/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 exac

[LeetCode]30. KSum问题总结

[LeetCode]1. 2Sum题目:https://leetcode.com/problems/two-sum/,解答:http://www.cnblogs.com/aprilcheny/p/4823576.html: [LeetCode]2. 3Sum题目:https://leetcode.com/problems/3sum/,解答:http://www.cnblogs.com/aprilcheny/p/4872283.html: [LeetCode]3. 3Sum Closest题目:h

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int