Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e08f0cf

Browse files
aQuaaQua
aQua
authored and
aQua
committedAug 14, 2017
problem 46
通过单元测试
1 parent 2b4b2e6 commit e08f0cf

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
package Problem0046
22

3+
func permute(nums []int) [][]int {
4+
ress := [][]int{}
5+
6+
makePermute(nums, []int{}, &ress)
7+
8+
return ress
9+
}
10+
11+
func makePermute(nums, res []int, ress *[][]int) {
12+
if len(nums) == 0 {
13+
*ress = append(*ress, res)
14+
}
15+
16+
for i, n := range nums {
17+
temp := make([]int, 0, len(nums))
18+
temp = append(temp, nums[:i]...)
19+
temp = append(temp, nums[i+1:]...)
20+
21+
makePermute(temp, append(res[:len(res):len(res)], n), ress)
22+
}
23+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0046
22

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

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

2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans struct {
24-
one string
24+
one [][]int
2525
}
2626

2727
func Test_Problem0046(t *testing.T) {
@@ -30,17 +30,24 @@ func Test_Problem0046(t *testing.T) {
3030
qs := []question{
3131

3232
question{
33-
para{""},
34-
ans{""},
33+
para{[]int{1, 2, 3}},
34+
ans{[][]int{
35+
[]int{1, 2, 3},
36+
[]int{1, 3, 2},
37+
[]int{2, 1, 3},
38+
[]int{2, 3, 1},
39+
[]int{3, 1, 2},
40+
[]int{3, 2, 1},
41+
}},
3542
},
36-
43+
3744
// 如需多个测试,可以复制上方元素。
3845
}
3946

4047
for _, q := range qs {
4148
a, p := q.ans, q.para
4249
fmt.Printf("~~%v~~\n", p)
4350

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
51+
ast.Equal(a.one, permute(p.one), "输入:%v", p)
4552
}
4653
}

0 commit comments

Comments
 (0)
Please sign in to comment.