Skip to content

Commit dfe53ae

Browse files
aQuaaQua
aQua
authored and
aQua
committed
78 accepted
1 parent f5f2fd1 commit dfe53ae

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

Algorithms/0078.subsets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ If nums = [1,2,3], a solution is:
2222
```
2323
## 解题思路
2424

25+
2526
见程序注释

Algorithms/0078.subsets/subsets.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package Problem0078
22

3+
import (
4+
"sort"
5+
)
6+
37
func subsets(nums []int) [][]int {
48
res := [][]int{}
59

@@ -9,13 +13,14 @@ func subsets(nums []int) [][]int {
913
}
1014

1115
func cur(nums, temp []int, res *[][]int) {
12-
if len(nums) == 0 {
16+
l := len(nums)
17+
if l == 0 {
18+
sort.Ints(temp)
1319
*res = append(*res, temp)
1420
return
1521
}
1622

17-
cur(nums[1:], temp, res)
18-
19-
cur(nums[1:], append(temp, nums[0]), res)
23+
cur(nums[:l-1], temp, res)
2024

25+
cur(nums[:l-1], append([]int{nums[l-1]}, temp...), res)
2126
}

Algorithms/0078.subsets/subsets_test.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,56 @@ func Test_Problem0078(t *testing.T) {
3333
},
3434
ans{[][]int{
3535
[]int{},
36-
[]int{3},
37-
[]int{2},
38-
[]int{2, 3},
3936
[]int{1},
40-
[]int{1, 3},
37+
[]int{2},
4138
[]int{1, 2},
39+
[]int{3},
40+
[]int{1, 3},
41+
[]int{2, 3},
4242
[]int{1, 2, 3},
4343
}},
4444
},
4545

46+
question{
47+
para{
48+
[]int{9,0,3,5,7},
49+
},
50+
ans{[][]int{
51+
[]int{},
52+
[]int{9},
53+
[]int{0},
54+
[]int{0,9},
55+
[]int{3},
56+
[]int{3,9},
57+
[]int{0,3},
58+
[]int{0,3,9},
59+
[]int{5},
60+
[]int{5,9},
61+
[]int{0,5},
62+
[]int{0,5,9},
63+
[]int{3,5},
64+
[]int{3,5,9},
65+
[]int{0,3,5},
66+
[]int{0,3,5,9},
67+
[]int{7},
68+
[]int{7,9},
69+
[]int{0,7},
70+
[]int{0,7,9},
71+
[]int{3,7},
72+
[]int{3,7,9},
73+
[]int{0,3,7},
74+
[]int{0,3,7,9},
75+
[]int{5,7},
76+
[]int{5,7,9},
77+
[]int{0,5,7},
78+
[]int{0,5,7,9},
79+
[]int{3,5,7},
80+
[]int{3,5,7,9},
81+
[]int{0,3,5,7},
82+
[]int{0,3,5,7,9},
83+
}},
84+
},
85+
4686
// 如需多个测试,可以复制上方元素。
4787
}
4888

0 commit comments

Comments
 (0)