Skip to content

Commit 3741ffe

Browse files
committed
feat(solutions): add subsets
1 parent e0422ba commit 3741ffe

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2727
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
28+
|78|[Subsets][Solutions-78]|[WindomZ][Solutions-78-golang]|Medium|[447 ns/op/5 test cases][Solutions-78-Test]|6 ms|
2829
|77|[Combinations][Solutions-77]|[WindomZ][Solutions-77-golang]|Medium|[336 ns/op/6 test cases][Solutions-77-Test]|246 ms|
2930
|75|[Sort Colors][Solutions-75]|[WindomZ][Solutions-75-golang]|Medium|[13.4 ns/op/6 test cases][Solutions-75-Test]|3 ms|
3031
|74|[Search a 2D Matrix][Solutions-74]|[WindomZ][Solutions-74-golang]|Medium|[32.5 ns/op/6 test cases][Solutions-74-Test]|16 ms|
@@ -129,6 +130,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
129130
### Support
130131
If you like it then you can put a :star:Star on it.
131132

133+
[Solutions-78]:https://leetcode.com/problems/subsets/
134+
[Solutions-78-golang]:solutions/subsets/subsets.go
135+
[Solutions-78-Test]:solutions/subsets/subsets_test.go#L62
132136
[Solutions-77]:https://leetcode.com/problems/combinations/
133137
[Solutions-77-golang]:solutions/combinations/combinations.go
134138
[Solutions-77-Test]:solutions/combinations/combinations_test.go#L32

solutions/subsets/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 78. Subsets
2+
3+
[Description](https://leetcode.com/problems/subsets/description/) |
4+
[Discuss](https://leetcode.com/problems/subsets/discuss/) |
5+
[Solution](https://leetcode.com/problems/subsets/solution/)
6+
7+
## Description
8+
9+
Given a set of **distinct** integers, _nums_, return all possible subsets (the power set).
10+
11+
**Note:** The solution set must not contain duplicate subsets.
12+
13+
For example,
14+
15+
If **_nums_** = `[1,2,3]`, a solution is:
16+
```
17+
[
18+
[3],
19+
[1],
20+
[2],
21+
[1,2,3],
22+
[1,3],
23+
[2,3],
24+
[1,2],
25+
[]
26+
]
27+
```

solutions/subsets/subsets.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package subsets
2+
3+
// like: {1, 2, 3}
4+
// 0) 0 0 0 -> {}
5+
// 1) 0 0 1 -> {1}
6+
// 2) 0 1 0 -> {2}
7+
// 3) 0 1 1 -> {1,2}
8+
// 4) 1 0 0 -> {3}
9+
// 5) 1 0 1 -> {1,3}
10+
// 6) 1 1 0 -> {2,3}
11+
// 7) 1 1 1 -> {1,2,3}
12+
13+
func subsets(nums []int) [][]int {
14+
if len(nums) == 0 {
15+
return [][]int{}
16+
}
17+
18+
count := 2
19+
for i := 1; i < len(nums); i++ {
20+
count *= 2 // count of result is 2^N
21+
}
22+
result := make([][]int, count)
23+
result[0] = []int{}
24+
25+
for i := 0; i < len(nums); i++ {
26+
for j := 1; j < count; j++ {
27+
if (uint(j)>>uint(i))&1 != 0 { // 2^N possible outcomes
28+
result[j] = append(result[j], nums[i])
29+
}
30+
}
31+
}
32+
return result
33+
}

solutions/subsets/subsets_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package subsets
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_subsets(t *testing.T) {
10+
assert.Equal(t,
11+
[][]int{}, subsets([]int{}))
12+
13+
assert.Equal(t,
14+
[][]int{
15+
{},
16+
{1},
17+
{2},
18+
{1, 2},
19+
}, subsets([]int{1, 2}))
20+
21+
assert.Equal(t,
22+
[][]int{
23+
{},
24+
{2},
25+
{3},
26+
{2, 3},
27+
}, subsets([]int{2, 3}))
28+
29+
assert.Equal(t,
30+
[][]int{
31+
{},
32+
{1},
33+
{2},
34+
{1, 2},
35+
{3},
36+
{1, 3},
37+
{2, 3},
38+
{1, 2, 3},
39+
}, subsets([]int{1, 2, 3}))
40+
41+
assert.Equal(t,
42+
[][]int{
43+
{},
44+
{1},
45+
{2},
46+
{1, 2},
47+
{3},
48+
{1, 3},
49+
{2, 3},
50+
{1, 2, 3},
51+
{5},
52+
{1, 5},
53+
{2, 5},
54+
{1, 2, 5},
55+
{3, 5},
56+
{1, 3, 5},
57+
{2, 3, 5},
58+
{1, 2, 3, 5},
59+
}, subsets([]int{1, 2, 3, 5}))
60+
}
61+
62+
func Benchmark_subsets(b *testing.B) {
63+
b.StopTimer()
64+
b.ReportAllocs()
65+
b.StartTimer()
66+
b.RunParallel(func(pb *testing.PB) {
67+
for pb.Next() {
68+
subsets([]int{})
69+
subsets([]int{1})
70+
subsets([]int{2, 3})
71+
subsets([]int{1, 2, 3})
72+
subsets([]int{1, 2, 3, 5})
73+
}
74+
})
75+
}

0 commit comments

Comments
 (0)