mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-09 20:32:07 +08:00
规范格式
This commit is contained in:
35
leetcode/0090.Subsets-II/90. Subsets II.go
Normal file
35
leetcode/0090.Subsets-II/90. Subsets II.go
Normal file
@ -0,0 +1,35 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func subsetsWithDup(nums []int) [][]int {
|
||||
c, res := []int{}, [][]int{}
|
||||
sort.Ints(nums) // 这里是去重的关键逻辑
|
||||
for k := 0; k <= len(nums); k++ {
|
||||
generateSubsetsWithDup(nums, k, 0, c, &res)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func generateSubsetsWithDup(nums []int, k, start int, c []int, res *[][]int) {
|
||||
if len(c) == k {
|
||||
b := make([]int, len(c))
|
||||
copy(b, c)
|
||||
*res = append(*res, b)
|
||||
return
|
||||
}
|
||||
// i will at most be n - (k - c.size()) + 1
|
||||
for i := start; i < len(nums)-(k-len(c))+1; i++ {
|
||||
fmt.Printf("i = %v start = %v c = %v\n", i, start, c)
|
||||
if i > start && nums[i] == nums[i-1] { // 这里是去重的关键逻辑,本次不取重复数字,下次循环可能会取重复数字
|
||||
continue
|
||||
}
|
||||
c = append(c, nums[i])
|
||||
generateSubsetsWithDup(nums, k, i+1, c, res)
|
||||
c = c[:len(c)-1]
|
||||
}
|
||||
return
|
||||
}
|
47
leetcode/0090.Subsets-II/90. Subsets II_test.go
Normal file
47
leetcode/0090.Subsets-II/90. Subsets II_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question90 struct {
|
||||
para90
|
||||
ans90
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para90 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans90 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem90(t *testing.T) {
|
||||
|
||||
qs := []question90{
|
||||
|
||||
question90{
|
||||
para90{[]int{}},
|
||||
ans90{[][]int{[]int{}}},
|
||||
},
|
||||
|
||||
question90{
|
||||
para90{[]int{1, 2, 2}},
|
||||
ans90{[][]int{[]int{}, []int{1}, []int{2}, []int{1, 2}, []int{2, 2}, []int{1, 2, 2}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 90------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans90, q.para90
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, subsetsWithDup(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
33
leetcode/0090.Subsets-II/README.md
Executable file
33
leetcode/0090.Subsets-II/README.md
Executable file
@ -0,0 +1,33 @@
|
||||
# [90. Subsets II](https://leetcode.com/problems/subsets-ii/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a collection of integers that might contain duplicates, ***nums***, return all possible subsets (the power set).
|
||||
|
||||
**Note:** The solution set must not contain duplicate subsets.
|
||||
|
||||
**Example:**
|
||||
|
||||
Input: [1,2,2]
|
||||
Output:
|
||||
[
|
||||
[2],
|
||||
[1],
|
||||
[1,2,2],
|
||||
[2,2],
|
||||
[1,2],
|
||||
[]
|
||||
]
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题是第 78 题的加强版,比第 78 题多了一个条件,数组中的数字会出现重复。
|
||||
- 解题方法依旧是 DFS,需要在回溯的过程中加上一些判断。
|
||||
- 这一题和第 78 题,第 491 题类似,可以一起解答和复习。
|
||||
|
Reference in New Issue
Block a user