详细见:leetcode.com/problems/3sum-closest
Java Solution: github
package leetcode;
import java.util.Arrays;
public class P016_3SumClosest {
public static void main(String[] args) {
// System.out.println(new Solution().threeSumClosest(new int[]{-1,2,1,-4}, 1));
// System.out.println(new Solution().threeSumClosest(new int[]{0, 2, 1, -3}, 1));
System.out.println(new Solution().threeSumClosest(new int[]{1,1,-1,-1,3}, -1));
}
/*
* 12 ms
* 67.05%
*/
static class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3)
return 0;
int ans = nums[0] + nums[1] + nums[2];
int cut = Integer.MAX_VALUE;
Arrays.sort(nums);
int i = 0, j = 0, k = 0;
for (i = 0; i != nums.length; i ++) {
j = i + 1; k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
int real = sum - target;
int tmp = Math.abs(real);
if (real == 0) {
return target;
} else if (tmp < cut) {
ans = sum;
cut = tmp;
if (real < cut)
j ++;
else
k --;
} else if (tmp == cut) {
if (real < cut)
j ++;
else
k --;
} else {
if (real < cut)
j ++;
else
k --;
}
}
}
return ans;
}
}
}
C Solution: github
/*
url: leetcode.com/problems/3sum-closest/
19ms 20.59%
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
void swap(int* a, int* b) {
int t = *a;
printf("a is %d\t b is %d\r\n", *a, *b);
*a = *b;
*b = t;
}
void bubble_sort(int *n, int s) {
int i = 0, sign = 1;
while (sign) {
sign = 0;
for (i = 1; i < s; i ++) {
if (*(n + i - 1) > *(n + i)) {
swap(n + i - 1, n + i);
sign = 1;
}
}
}
}
int threeSumClosest(int* nums, int numsSize, int target) {
int answer = 0, sum = 0;
int i = 0, l = 0, r = 0;
if (numsSize < 1) return 0;
bubble_sort(nums, numsSize);
answer = *(nums + 0) + *(nums + 1) + *(nums + 2);
for (i = 0; i < numsSize; i ++) {
l = i + 1;
r = numsSize - 1;
while (l < r) {
//printf("l is %d\tr is %d\r\n", l, r);
sum = *(nums + i) + *(nums + l) + *(nums + r);
if (sum == target) {
return target;
} else if (sum < target) {
answer = abs(answer - target) > abs(sum - target) ? sum : answer;
l ++;
} else {
answer = abs(answer - target) > abs(sum - target) ? sum : answer;
r --;
}
}
}
return answer;
}
int main() {
int nums[] = {1, 1, 1, 1};
int numsSize = 4;
int target = 0;
printf("answer is %d\r\n", threeSumClosest(nums, numsSize, target));
}
#coding=utf-8
'''
url: leetcode.com/problems/3sum-closest
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年3月28日
@details: Solution: AC 199ms 33.81%
'''
class Solution(object):
def threeSumClosest(self, n, t):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
ln = 0 if n == None else len(n)
if ln < 3: return 0
n.sort(key=None, reverse=False)
a = n[0] + n[1] + n[2]
ran = abs(a - t)
for i in range(ln):
if i != 0 and n[i - 1] == n[i]: continue
l, r = i + 1, ln - 1
while l < r:
s = n[i] + n[l] + n[r]
if s == t:
return t
if abs(s - t) < ran:
a = s
ran = abs(s - t)
if s < t:
while True:
l += 1
if l >= r or n[l - 1] != n[l]:
break
else:
while True:
r -= 1
if l >= r or n[r + 1] != n[r]:
break
return a
if __name__ == "__main__":
n = [-1, 2, 1, -4]
t = -2
sol = Solution()
print(sol.threeSumClosest(n, t))