【LeetNode2-1-9】Three sum closest--C++源代码(VS2015)
来源:互联网 发布:免费邮件服务器软件 编辑:程序博客网 时间:2024/05/12 07:29
#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;
}
阅读全文
0 0
- 【LeetNode2-1-9】Three sum closest--C++源代码(VS2015)
- 【LeetNode2-1-8】Three sum--C++源代码(VS2015)
- 【LeetNode2-1-7】Two sum--C++源代码(VS2015)
- 【LeetNode2-1-10】Four sum--C++源代码(VS2015)
- 【LeetNode2-1-6】Longest consecutive sequence--C++源代码(VS2015)
- three sum closest
- three Sum Closest
- 【LeetNode2-1-1】Remove duplicates from sorted array I--C++源代码(VS2015)
- 【LeetNode2-1-2】Remove duplicates from sorted array II--C++源代码(VS2015)
- 【LeetNode2-1-3】Search in rotated sorted array I--C++源代码(VS2015)
- 【LeetNode2-1-4】Search in rotated sorted array II--C++源代码(VS2015)
- 【LeetNode2-1-5】Median of two sorted arrays--C++源代码(VS2015)
- LeetCode (1) Two Sum (15)Three sum (16)Three sum closest (17)Four sum
- leetcode 1|167.two sum 1|2 && 3.three sum && 16.3Sum Closest && 18. 4Sum
- 9: 3Sum Closest
- 2.1.9 3Sum Closest
- Leetcode c语言-3Sum Closest
- Leetcode Exercise 1:Two Sum + Three Sum
- UVALive 7281 Saint John Festival (求凸包+判断点是否在凸包内(O(logn)复杂度))
- 安装windows+linux双系统问题记录
- 从【poj1275/HDU1529】总结差分约束
- Codeforces Round #438 (Div. 1 + Div. 2 combined) A. Bark to Unlock(模拟)
- Web Service (007---JAVA自动生成asp的WebService接口相关问题)
- 【LeetNode2-1-9】Three sum closest--C++源代码(VS2015)
- Qt笔记_6
- 基于table的Q learning和Sarsa算法
- java序列化与反序列化(3)------jdk原生序列化机制Externalizable
- MySQL查询语句练习题(50题版)
- Xilinx FPGA 学习笔记——时钟资源
- 浅谈卡尔曼滤波器
- board (双联通分量)
- telnet服务开启