-
Notifications
You must be signed in to change notification settings - Fork 0
/
3SumClosest.cpp
66 lines (57 loc) · 2.05 KB
/
3SumClosest.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
//
// 3SumClosest.cpp
//
// tag: sort
// Created by 廷芳 杜 on 7/7/13.
// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//
// Similar to 3Sum problem.
// Because maybe there is no triplet that adds up to target. The "closest Value" could happen at the both side of the target.
// Solution: Each time, compare the sum to the current closest value, if sum is closer to the target, update the value.
// That's it.
// Tc O(n^2). SC O(1)
#include <iostream>
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( num.size() == 0 ) return 0;
if( num.size() <= 3 )
{
int sum=0;
for( int i = 0 ; i < num.size(); i ++ )
sum = sum + num[i];
return sum;
}
sort( num.begin(), num.end() );
int sum = 0;
int start = 1;
int end = num.size()-1;
int closestValue = num[0] + num[start] + num[end];
for( int i = 0 ; i < num.size()-2; i++ )
{
start = i + 1;
sum = num[i] + num[start] + num[end];
if( sum == target ) return sum;
while( end>start+1 && num[i] + num[start] + num[end] < target )
{
sum = num[i]+num[start]+num[end];
if( abs(target - closestValue ) > abs(target - sum) )
closestValue = sum;
start++;
}
while( end>start+1 && num[i] + num[start] + num[end] > target )
{
sum = num[i]+num[start]+num[end];
if( abs(target - closestValue ) > abs(target - sum) )
closestValue = sum;
end--;
}
sum = num[i] + num[start] + num[end] ;
if( abs(target - closestValue) > abs(target - sum) )
closestValue = sum;
}
return closestValue;
}
};