Skip to content

Commit 501bf84

Browse files
committed
[E:69/533, M:69/974, H:6/387] add No: 78: Subsets
1 parent fbe69ad commit 501bf84

File tree

17 files changed

+568
-0
lines changed

17 files changed

+568
-0
lines changed

.leetcode.db

0 Bytes
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## [子集](https://leetcode-cn.com/problems/subsets/)
2+
3+
给定一组**不含重复元素**的整数数组 _nums_,返回该数组所有可能的子集(幂集)。
4+
5+
**说明:**解集不能包含重复的子集。
6+
7+
**示例:**
8+
9+
`**输入:** nums = [1,2,3]
10+
**输出:**
11+
[
12+
[3],
13+
  [1],
14+
  [2],
15+
  [1,2,3],
16+
  [1,3],
17+
  [2,3],
18+
  [1,2],
19+
  []
20+
]`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/serial/medium/78/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[1,2,3]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input [][]int
23+
want bool
24+
}{
25+
{
26+
name: "test-[[1],[2],[3],[]]",
27+
input: [][]int{
28+
{1},
29+
{2},
30+
{3},
31+
{},
32+
},
33+
want: true,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
got := test.want
45+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
46+
got = solution.Export(test.input)
47+
cancel()
48+
})
49+
50+
if timeout {
51+
testLog.Fail(idx+1, test.name, "timeout")
52+
continue
53+
}
54+
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(nums []int) [][]int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", nums)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return subsets(nums)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func subsets(nums []int) [][]int {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(nums []int) [][]int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", nums)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return subsets(nums)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func subsets(nums []int) [][]int {
29+
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## [子集](https://leetcode-cn.com/problems/subsets/)
2+
3+
给定一组**不含重复元素**的整数数组 _nums_,返回该数组所有可能的子集(幂集)。
4+
5+
**说明:**解集不能包含重复的子集。
6+
7+
**示例:**
8+
9+
`**输入:** nums = [1,2,3]
10+
**输出:**
11+
[
12+
[3],
13+
  [1],
14+
  [2],
15+
  [1,2,3],
16+
  [1,3],
17+
  [2,3],
18+
  [1,2],
19+
  []
20+
]`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/tags/位运算/medium/subsets/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[1,2,3]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input [][]int
23+
want bool
24+
}{
25+
{
26+
name: "test-[[1],[2],[3],[]]",
27+
input: [][]int{
28+
{1},
29+
{2},
30+
{3},
31+
{},
32+
},
33+
want: true,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
got := test.want
45+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
46+
got = solution.Export(test.input)
47+
cancel()
48+
})
49+
50+
if timeout {
51+
testLog.Fail(idx+1, test.name, "timeout")
52+
continue
53+
}
54+
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(nums []int) [][]int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", nums)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return subsets(nums)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func subsets(nums []int) [][]int {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(nums []int) [][]int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", nums)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return subsets(nums)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func subsets(nums []int) [][]int {
29+
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## [子集](https://leetcode-cn.com/problems/subsets/)
2+
3+
给定一组**不含重复元素**的整数数组 _nums_,返回该数组所有可能的子集(幂集)。
4+
5+
**说明:**解集不能包含重复的子集。
6+
7+
**示例:**
8+
9+
`**输入:** nums = [1,2,3]
10+
**输出:**
11+
[
12+
[3],
13+
  [1],
14+
  [2],
15+
  [1,2,3],
16+
  [1,3],
17+
  [2,3],
18+
  [1,2],
19+
  []
20+
]`

0 commit comments

Comments
 (0)