-
Notifications
You must be signed in to change notification settings - Fork 1
/
16_3_Sum_Closest.cpp
67 lines (59 loc) · 1.77 KB
/
16_3_Sum_Closest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
/**
* @brief The Solution class
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
using namespace std;
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
size_t len=nums.size();
int temp=0, res=nums.at(0)+nums.at(1)+nums.at(2);
for(size_t i=0; i<len; i++)
{
for(size_t j=i+1, k=len-1; j<k; )
{
temp = nums.at(i) + nums.at(j) + nums.at(k);
res = (abs(res-target) > abs(temp-target) ? temp : res);
if(temp > target)
k--;
else if(temp < target)
j++;
else
{
j++;
k--;
}
}
}
return res;
}
void test(vector<int> &nums, int &target, int &ans)
{
int res = threeSumClosest(nums, target);
cout << res << (res == ans ? " == " : " != ") << ans << ":";
for(size_t i=0; i<nums.size(); i++)
cout << nums.at(i) << " ";
cout << endl;
}
};
int main()
{
vector<vector<int>> strs = {{-1, 2, 1, -4};
vector<int> targets = {1};
vector<int> ans = {2};
for(size_t i=0; i<ans.size(); i++)
{
Solution example;
example.test(strs.at(i), targets.at(i), ans.at(i));
}
return 0;
}