首页 > 代码库 > Leetcode 3Sum Closet

Leetcode 3Sum Closet

用了和3Sum差不多一样的思路,二分查找。关键要剪枝,但是却在剪枝那里犯了很多错误。

然后原来有一个更加快的思路O(n^2).

#include<iostream>  
#include <vector>  
#include <algorithm>  
#include <math.h>
using namespace std;


class Solution {
public:

	int threeSumClosest(vector<int> &num, int target) {

		int minDis = 1 << 30;
		int closetSum = -1<<30;

		sort(num.begin(), num.end());
		for (int i = 0; i < num.size() - 2; i++)
		{
			for (int j = i + 1; j < num.size() - 1; j++)
			{

				int twoSum = num[i] + num[j];
				int value = http://www.mamicode.com/target - twoSum;>

Leetcode 3Sum Closet