-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSumOfThreeNum.js
41 lines (41 loc) · 1.38 KB
/
findSumOfThreeNum.js
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
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function (nums, target) {
if (nums.length < 3) {
return false
} else if (nums.length == 3) {
return nums.reduce((sum, value) => {
return sum + value
}, 0)
} else {
let orderNums = quickSort(nums)
let temp = 0, close = Number.MAX_VALUE
let sum = 0
for (let i = 0; i < orderNums.length - 2; i++) {
let front = i + 1, high = orderNums.length - 1
while (front < high) {
temp = orderNums[i] + orderNums[front] + orderNums[high]
if (temp < target) {
if (target - temp < close) {
close = target - temp
sum = temp
}
front++
} else if (temp > target) {
if (temp - target < close) {
close = temp - target
sum = temp
}
high--
} else {
sum = target
return sum
}
}
}
return sum
}
};