Toc
  1. Q:
  2. A:
Toc
0 results found
bbcfive
3Sum Closest
2019/06/28 Algorithm Array

Q:

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).

A:

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
/**
* 3Sum Closest
* 最接近的三数之和
*/

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/

function binarySearch(a, target) {
var start = 0
, end = a.length - 1;

while(start <= end) {
var mid = ~~((start + end) >> 1);
if (a[mid] >= target)
end = mid - 1;
else
start = mid + 1;
}

return start;
}


var threeSumClosest = function(nums, target) {
nums.sort(function(a, b) {
return a - b;
});

var len = nums.length;
var ans = Infinity;

for (var i = 0; i < len; i++)
for (var j = i + 1; j < len; j++) {
var a = target - nums[i] - nums[j];
var pos = binarySearch(nums, a);

for (var k = Math.max(0, pos - 1); k <= Math.min(pos + 0, len - 1); k++) {
if (k === i || k === j)
continue;

var sum = nums[i] + nums[j] + nums[k];
if (Math.abs(sum - target) < Math.abs(ans - target))
ans = sum;
}

}

return ans;
};
本文作者:bbcfive
版权声明:本文首发于bbcfive的博客,转载请注明出处!