-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3sum-closest.ts
47 lines (41 loc) · 1.2 KB
/
3sum-closest.ts
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
export function threeSumClosest (nums: number[], target: number): number {
nums.sort((a, b) => a - b)
let res = nums[0] + nums[1] + nums[2]
for (let i = 0, len = nums.length - 2; i < len && res !== target; i++) {
let left = i + 1
let right = len + 1
while (left !== right) {
// 判断最小值
let min = nums[i] + nums[left] + nums[left + 1]
if (target < min) {
if (Math.abs(min - target) < Math.abs(res - target)) res = min
break
}
//判断最大值
let max = nums[i] + nums[right] + nums[right - 1]
if (target > max) {
if (Math.abs(max - target) < Math.abs(res - target)) res = max
break
}
let sum = nums[i] + nums[left] + nums[right]
if (Math.abs(sum - target) < Math.abs(res - target)) {
res = sum
}
if (sum > target) {
right--
while (left !== right && nums[right] === nums[right + 1]) { // 去重
right--
}
} else {
left++
while (left !== right && nums[left] === nums[left - 1]) { // 去重
left++
}
}
while (i < len && nums[i] == nums[i + 1]) { // 去重
i++
}
}
}
return res
}