-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsets.go
85 lines (73 loc) · 1.55 KB
/
subsets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"strconv"
)
// source: https://leetcode.com/problems/subsets/
func subsets(nums []int) [][]int {
ans := [][]int{{}}
n := len(nums)
for i := int64(1); i < 1<<n; i++ {
cur := make([]int, 0)
bitmask := strconv.FormatInt(i, 2)
for j, b := range bitmask {
if b == '1' {
cur = append(cur, nums[len(bitmask)-j-1])
}
}
ans = append(ans, cur)
}
return ans
}
// solution using backtracking
func subsets_(nums []int) [][]int {
n := len(nums)
ans := [][]int{{}}
var backtrack func(s, size int, cur []int)
backtrack = func(s, size int, cur []int) {
if len(cur) == size {
ans = append(ans, make([]int, size))
copy(ans[len(ans)-1], cur)
return
}
for i := s; i < n; i++ {
cur = append(cur, nums[i])
backtrack(i+1, size, cur)
cur = cur[:len(cur)-1]
}
}
for i := 1; i <= n; i++ {
backtrack(0, i, make([]int, 0))
}
return ans
}
func main() {
testCases := []struct {
nums []int
want [][]int
}{
{
nums: []int{1, 2, 3},
want: [][]int{{}, {1}, {2}, {1, 2}, {3}, {1, 3}, {2, 3}, {1, 2, 3}},
},
{
nums: []int{0},
want: [][]int{{}, {0}},
},
}
successes := 0
for _, tc := range testCases {
x := subsets(tc.nums)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}