mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
90.子集II增加Go使用used数组解法
This commit is contained in:
@ -310,6 +310,43 @@ class Solution:
|
||||
```
|
||||
### Go
|
||||
|
||||
使用used数组
|
||||
```Go
|
||||
var (
|
||||
result [][]int
|
||||
path []int
|
||||
)
|
||||
|
||||
func subsetsWithDup(nums []int) [][]int {
|
||||
result = make([][]int, 0)
|
||||
path = make([]int, 0)
|
||||
used := make([]bool, len(nums))
|
||||
sort.Ints(nums) // 去重需要排序
|
||||
backtracing(nums, 0, used)
|
||||
return result
|
||||
}
|
||||
|
||||
func backtracing(nums []int, startIndex int, used []bool) {
|
||||
tmp := make([]int, len(path))
|
||||
copy(tmp, path)
|
||||
result = append(result, tmp)
|
||||
for i := startIndex; i < len(nums); i++ {
|
||||
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
|
||||
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
|
||||
// 而我们要对同一树层使用过的元素进行跳过
|
||||
if i > 0 && nums[i] == nums[i-1] && used[i-1] == false {
|
||||
continue
|
||||
}
|
||||
path = append(path, nums[i])
|
||||
used[i] = true
|
||||
backtracing(nums, i + 1, used)
|
||||
path = path[:len(path)-1]
|
||||
used[i] = false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
不使用used数组
|
||||
```Go
|
||||
var (
|
||||
path []int
|
||||
|
Reference in New Issue
Block a user