Skip to content

Commit 86ecb4f

Browse files
aQuaaQua
aQua
authored and
aQua
committed
added Problem 15
1 parent 2a1b615 commit 86ecb4f

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

Algorithms/0015.3sum/3sum.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
11
package Problem0015
22

3-
import (
4-
"sort"
5-
)
3+
import "sort"
64

75
func threeSum(nums []int) [][]int {
6+
// 排序后,可以按规律查找
7+
sort.Ints(nums)
88
res := [][]int{}
99

10-
for i := 0; i < len(nums)-2; i++ {
11-
for j := i + 1; j < len(nums)-1; j++ {
12-
for k := j + 1; k < len(nums); k++ {
13-
if nums[i]+nums[j]+nums[k] == 0 {
14-
temp := []int{nums[i], nums[j], nums[k]}
15-
if yes := contain(res, temp); !yes {
16-
res = append(res, temp)
10+
for i := range nums {
11+
// 避免添加重复的结果
12+
if i > 0 && nums[i] == nums[i-1] {
13+
continue
14+
}
1715

18-
}
19-
}
16+
l, r := i+1, len(nums)-1
17+
18+
for l < r {
19+
s := nums[i] + nums[l] + nums[r]
20+
switch {
21+
case s < 0:
22+
// 较小的 l 需要变大
23+
l++
24+
case s > 0:
25+
// 较大的 r 需要变小
26+
r--
27+
default:
28+
res = append(res, []int{nums[i], nums[l], nums[r]})
29+
// 为避免重复添加,l 和 r 都需要移动到不同的元素上。
30+
l, r = next(nums, l, r)
2031
}
2132
}
2233
}
2334

2435
return res
2536
}
2637

27-
func contain(numss [][]int, nums []int) bool {
28-
sort.Ints(nums)
29-
30-
for _, ns := range numss {
31-
sort.Ints(ns)
32-
if equal(ns, nums) {
33-
return true
38+
func next(nums []int, l, r int) (int, int) {
39+
for l < r {
40+
switch {
41+
case nums[l] == nums[l+1]:
42+
l++
43+
case nums[r] == nums[r-1]:
44+
r--
45+
default:
46+
l++
47+
r--
48+
return l, r
3449
}
3550
}
3651

37-
return false
38-
}
39-
40-
func equal(a, b []int) bool {
41-
for i, v := range a {
42-
if v != b[i] {
43-
return false
44-
}
45-
}
46-
return true
52+
return l, r
4753
}

Algorithms/0015.3sum/3sum_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Problem0015
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -29,10 +30,36 @@ func Test_Problem0015(t *testing.T) {
2930
qs := []question{
3031

3132
question{
32-
para{[]int{-1, 0, 1, 2, -1, -4}},
33+
para{[]int{1, -1, -1, 0}},
3334
ans{[][]int{
3435
[]int{-1, 0, 1},
36+
}},
37+
},
38+
39+
question{
40+
para{[]int{-1, 0, 1, 2, 2, 2, 2, -1, -4}},
41+
ans{[][]int{
42+
[]int{-4, 2, 2},
3543
[]int{-1, -1, 2},
44+
[]int{-1, 0, 1},
45+
}},
46+
},
47+
question{
48+
para{[]int{0, 0, 0, 0, 0}},
49+
ans{[][]int{
50+
[]int{0, 0, 0},
51+
}},
52+
},
53+
question{
54+
para{[]int{1, 1, -2}},
55+
ans{[][]int{
56+
[]int{-2, 1, 1},
57+
}},
58+
},
59+
question{
60+
para{[]int{0, 0, 0}},
61+
ans{[][]int{
62+
[]int{0, 0, 0},
3663
}},
3764
},
3865

@@ -43,5 +70,6 @@ func Test_Problem0015(t *testing.T) {
4370
a, p := q.ans, q.para
4471

4572
ast.Equal(a.one, threeSum(p.one), "输入:%v", p)
73+
fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
4674
}
4775
}

Algorithms/0015.3sum/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# [15. 3Sum](https://leetcode.com/problems/3sum/)
22

33
## 题目
4+
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
45

6+
Note: The solution set must not contain duplicate triplets.
7+
```
8+
For example, given array S = [-1, 0, 1, 2, -1, -4],
59
10+
A solution set is:
11+
[
12+
[-1, 0, 1],
13+
[-1, -1, 2]
14+
]
15+
```
616
## 解题思路
17+
穷举法无法通过时间限制
718

8-
9-
## 总结
10-
11-
19+
详见代码

0 commit comments

Comments
 (0)