Skip to content

Commit e1b471a

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 16
试着往15上套,但是好像不大一样
1 parent c943c0e commit e1b471a

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
11
package Problem0016
22

3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func threeSumClosest(nums []int, target int) int {
9+
10+
// 排序后,可以按规律查找
11+
sort.Ints(nums)
12+
res, delta := 0, math.MaxInt64
13+
14+
for i := range nums {
15+
// 避免重复计算
16+
if i > 0 && nums[i] == nums[i-1] {
17+
continue
18+
}
19+
20+
l, r := i+1, len(nums)-1
21+
22+
for l < r {
23+
s := nums[i] + nums[l] + nums[r]
24+
temp := abs(target - s)
25+
switch {
26+
case temp < delta:
27+
28+
l++
29+
case s > 0:
30+
// 较大的 r 需要变小
31+
r--
32+
default:
33+
res = append(res, []int{nums[i], nums[l], nums[r]})
34+
// 为避免重复添加,l 和 r 都需要移动到不同的元素上。
35+
l, r = next(nums, l, r)
36+
}
37+
}
38+
}
39+
40+
return res
41+
}
42+
43+
func next(nums []int, l, r int) (int, int) {
44+
for l < r {
45+
switch {
46+
case nums[l] == nums[l+1]:
47+
l++
48+
case nums[r] == nums[r-1]:
49+
r--
50+
default:
51+
l++
52+
r--
53+
return l, r
54+
}
55+
}
56+
57+
return l, r
58+
}
59+
60+
func abs(a int) int {
61+
if a > 0 {
62+
return a
63+
}
64+
return -a
65+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0016
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,14 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one []int
19+
two int
1920
}
2021

2122
// ans 是答案
2223
// one 代表第一个答案
2324
type ans struct {
24-
one string
25+
one int
2526
}
2627

2728
func Test_Problem0016(t *testing.T) {
@@ -30,17 +31,17 @@ func Test_Problem0016(t *testing.T) {
3031
qs := []question{
3132

3233
question{
33-
para{""},
34-
ans{""},
34+
para{[]int{-1, 2, 1, -4}, 1},
35+
ans{2},
3536
},
36-
37+
3738
// 如需多个测试,可以复制上方元素。
3839
}
3940

4041
for _, q := range qs {
4142
a, p := q.ans, q.para
4243
fmt.Printf("~~%v~~\n", p)
4344

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
45+
ast.Equal(a.one, threeSumClosest(p.one, p.two), "输入:%v", p)
4546
}
4647
}

Algorithms/0016.3sum-closest/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# [16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/)
22

33
## 题目
4+
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
5+
```
6+
For example, given array S = {-1 2 1 -4}, and target = 1.
47
5-
8+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
9+
```
610
## 解题思路
711

812

0 commit comments

Comments
 (0)