-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3SumClosest16.cpp
More file actions
75 lines (68 loc) · 2.27 KB
/
3SumClosest16.cpp
File metadata and controls
75 lines (68 loc) · 2.27 KB
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
68
69
70
71
72
73
74
75
/*
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).
*/
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int small = INT_MAX, ans = target, temp;
std::sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) {
int find = target-nums[i];
int front = i + 1;
int back = nums.size() - 1;
while (front < back) {
int sum = nums[front] + nums[back];
temp = sum+nums[i];
if (abs(temp-target) < small) {
small = abs(temp-target);
ans = temp;
}
// Finding answer which start from number num[i]
if (sum < find) {
front++;
} else if (sum > find) {
back--;
} else if (sum == find) {
//cout <<"found !!" << endl;
return target;
}
}
}
return ans;
}
};
//the fatest method
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int closest = nums[0] + nums[1] + nums[2];
int sum;
int size = nums.size();
int j, k;
sort(nums.begin(), nums.end());
for (int i = 0; i < size-2; i++) {
j = i + 1;
k = size - 1;
while (j < k) {
sum = nums[i] + nums[j] + nums[k];
if (sum == target) {
return sum;
}
else if (sum > target) {
if (abs(sum-target) < abs(closest-target))
closest = sum;
k--;
}
else { // sum < target
if (abs(sum-target) < abs(closest-target))
closest = sum;
j++;
}
}
}
return closest;
}
};