【LeetNode2-1-9】Three sum closest--C++源代码(VS2015)
2017-10-05 16:45
495 查看
#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
/*
three sum closest
*/
int min(int Num_A,int Num_B)
{
return Num_A > Num_B ? Num_B : Num_A;
}
void QuickSort(vector<int> &vec,int left,int right)
{
if (vec.size() <= 1)
return;
if (left > right)
return;
int first = left;
int last = right;
int key = vec[first];
while (first < last)
{
while (first < last && vec[last] > key)
{
last--;
}
vec[first] = vec[last];
while (first < last && vec[first] < key)
{
first++;
}
vec[last] = vec[first];
}
vec[first] = key;
QuickSort(vec, left, first - 1);
QuickSort(vec, first + 1, right);
}
int Solution(vector<int> &vec,const int target)
{
int result = -1;
if (vec.size() < 3)
return result;
vector<int>::iterator i = vec.begin();
vector<int>::iterator k=vec.end()-1;
result = abs(*i + *(i + 1) + *k - target);
i++;
for (; i != vec.end() - 2; i++)
{
if (i > vec.begin() && *i == *(i - 1))
continue;
vector<int>::iterator j = i + 1;
while (j < k)
{
if (*i + *j + *k < target)
{
result = min(result, abs(*i + *j + *k - target));
j++;
while (*j == *(j - 1) && j < k)
{
j++;
}
}
else if (*i + *j + *k > target)
{
result = min(result, abs(*i + *j + *k - target));
k--;
while (*k == *(k + 1) && j < k)
{
k--;
}
}
else
{
return 0;
}
}
}
return result;
}
int main()
{
int arr[] = { 2,4,6,8,1,3,5,7 };
vector<int> vec(&arr[0], &arr[8]);
QuickSort(vec,0,vec.size()-1);
int result = Solution(vec,22);
cout << "result is " << result << endl;
system("pause");
return 0;
}
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
/*
three sum closest
*/
int min(int Num_A,int Num_B)
{
return Num_A > Num_B ? Num_B : Num_A;
}
void QuickSort(vector<int> &vec,int left,int right)
{
if (vec.size() <= 1)
return;
if (left > right)
return;
int first = left;
int last = right;
int key = vec[first];
while (first < last)
{
while (first < last && vec[last] > key)
{
last--;
}
vec[first] = vec[last];
while (first < last && vec[first] < key)
{
first++;
}
vec[last] = vec[first];
}
vec[first] = key;
QuickSort(vec, left, first - 1);
QuickSort(vec, first + 1, right);
}
int Solution(vector<int> &vec,const int target)
{
int result = -1;
if (vec.size() < 3)
return result;
vector<int>::iterator i = vec.begin();
vector<int>::iterator k=vec.end()-1;
result = abs(*i + *(i + 1) + *k - target);
i++;
for (; i != vec.end() - 2; i++)
{
if (i > vec.begin() && *i == *(i - 1))
continue;
vector<int>::iterator j = i + 1;
while (j < k)
{
if (*i + *j + *k < target)
{
result = min(result, abs(*i + *j + *k - target));
j++;
while (*j == *(j - 1) && j < k)
{
j++;
}
}
else if (*i + *j + *k > target)
{
result = min(result, abs(*i + *j + *k - target));
k--;
while (*k == *(k + 1) && j < k)
{
k--;
}
}
else
{
return 0;
}
}
}
return result;
}
int main()
{
int arr[] = { 2,4,6,8,1,3,5,7 };
vector<int> vec(&arr[0], &arr[8]);
QuickSort(vec,0,vec.size()-1);
int result = Solution(vec,22);
cout << "result is " << result << endl;
system("pause");
return 0;
}
相关文章推荐
- 【LeetNode2-1-6】Longest consecutive sequence--C++源代码(VS2015)
- 【LeetNode2-1-7】Two sum--C++源代码(VS2015)
- 【LeetNode2-1-8】Three sum--C++源代码(VS2015)
- 【LeetNode2-1-10】Four sum--C++源代码(VS2015)
- 【LeetNode2-1-1】Remove duplicates from sorted array I--C++源代码(VS2015)
- 【LeetNode2-1-2】Remove duplicates from sorted array II--C++源代码(VS2015)
- 【LeetNode2-1-4】Search in rotated sorted array II--C++源代码(VS2015)
- 【LeetNode2-1-5】Median of two sorted arrays--C++源代码(VS2015)
- 【算法】Shell排序--C++源代码(VS2015)
- 【算法】斐波那契数列--C++源代码(VS2015)
- 【设计】单例模式--C++源代码(VS2015)
- 【算法】快速排序--C++源代码(VS2015)
- 【基础】随机数生成--C++源代码(VS2015)
- 【基础】简析浅拷贝与深拷贝--C++源代码(VS2015)
- 【算法】分而治之(DivideAndConquer) -- C++源代码(VS2015)
- 【设计】工厂模式--C++源代码(VS2015)
- 【基础】大端小端(Big-endian&Little-endian)--C++源代码(VS2015)
- C++学习笔记——VS2015程序的简单调试
- [LeetCode]16.threeSumClosest
- 使用VS2015 创建C++动态链接库并调用