-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.cpp
More file actions
43 lines (42 loc) · 1.22 KB
/
16.cpp
File metadata and controls
43 lines (42 loc) · 1.22 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
//
// 16.cpp
// leetcode
//
// Created by R Z on 2018/1/30.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
int gap=INT_MAX;
int res = 0;
if(len==0) return 0;
sort(nums.begin(),nums.end());
for(int i=0; i<len-2; i++){
if(i==0 || (i>0 && nums[i]!=nums[i-1])){
int lo=i+1, hi=len-1, sum=target-nums[i];
while(lo<hi){
if(nums[lo]+nums[hi]==sum) return target;
else if(nums[lo]+nums[hi]<sum){
if(abs(nums[lo]+nums[hi]-sum)<gap){
gap=abs(nums[lo]+nums[hi]-sum);
res=target-gap;
}
lo++;
}else{
if(abs(nums[lo]+nums[hi]-sum)<gap){
gap=abs(nums[lo]+nums[hi]-sum);
res=target+gap;
}
hi--;
}
}
}
}
return res;
}
};