Skip to content

Commit 80c7613

Browse files
authored
Merge pull request #844 from 0xff-dev/90
Add solution and test-cases for problem 90
2 parents 4c1029a + 26e199c commit 80c7613

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [90.Subsets II][title]
2+
3+
## Description
4+
Given an integer array `nums` that may contain duplicates, return all possible
5+
subsets (the power set).
6+
7+
The solution set **must not** contain duplicate subsets. Return the solution in **any order**.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: nums = [1,2,2]
13+
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: nums = [0]
20+
Output: [[],[0]]
21+
```
22+
23+
## 结语
24+
25+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
26+
27+
[title]: https://leetcode.com/problems/subsets-ii
28+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) [][]int {
4+
ans := [][]int{{}}
5+
cache := make(map[[21]int]struct{})
6+
7+
var (
8+
dfs func(int, int, [21]int)
9+
toRes func([21]int) []int
10+
)
11+
12+
toRes = func(path [21]int) []int {
13+
r := make([]int, 0)
14+
for i := 0; i <= 10; i++ {
15+
if path[i] == 0 {
16+
continue
17+
}
18+
for count := path[i]; count > 0; count-- {
19+
r = append(r, i)
20+
}
21+
}
22+
for i := 11; i < 21; i++ {
23+
if path[i] == 0 {
24+
continue
25+
}
26+
for count := path[i]; count > 0; count-- {
27+
r = append(r, 10-i)
28+
}
29+
}
30+
return r
31+
}
32+
33+
dfs = func(index, l int, path [21]int) {
34+
if l == 0 {
35+
if _, ok := cache[path]; !ok {
36+
ans = append(ans, toRes(path))
37+
cache[path] = struct{}{}
38+
}
39+
return
40+
}
41+
if index == len(nums) {
42+
return
43+
}
44+
cur := nums[index]
45+
if cur < 0 {
46+
cur = 10 - cur
47+
}
48+
path[cur]++
49+
dfs(index+1, l-1, path)
50+
path[cur]--
51+
dfs(index+1, l, path)
52+
}
53+
for l := 1; l <= len(nums); l++ {
54+
dfs(0, l, [21]int{})
55+
}
56+
57+
return ans
558
}

leetcode/1-100/0090.Subsets-II/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
inputs []int
13+
expect [][]int
1414
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
15+
{"TestCacse1", []int{1, 2, 2}, [][]int{
16+
{}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2},
17+
}},
18+
{"TestCacse2", []int{0}, [][]int{{}, {0}}},
1819
}
1920

2021
// 开始测试
@@ -29,10 +30,10 @@ func TestSolution(t *testing.T) {
2930
}
3031
}
3132

32-
// 压力测试
33+
// 压力测试
3334
func BenchmarkSolution(b *testing.B) {
3435
}
3536

36-
// 使用案列
37+
// 使用案列
3738
func ExampleSolution() {
3839
}

0 commit comments

Comments
 (0)