Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fc1751

Browse files
Merge pull request youngyangyang04#2644 from markwang1992/90-subsetsWithDup
90.子集II增加Go使用used数组解法
2 parents 78c75e0 + 1c9f885 commit 3fc1751

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎problems/0090.子集II.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,43 @@ class Solution:
310310
```
311311
### Go
312312

313+
使用used数组
314+
```Go
315+
var (
316+
result [][]int
317+
path []int
318+
)
319+
320+
func subsetsWithDup(nums []int) [][]int {
321+
result = make([][]int, 0)
322+
path = make([]int, 0)
323+
used := make([]bool, len(nums))
324+
sort.Ints(nums) // 去重需要排序
325+
backtracing(nums, 0, used)
326+
return result
327+
}
328+
329+
func backtracing(nums []int, startIndex int, used []bool) {
330+
tmp := make([]int, len(path))
331+
copy(tmp, path)
332+
result = append(result, tmp)
333+
for i := startIndex; i < len(nums); i++ {
334+
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
335+
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
336+
// 而我们要对同一树层使用过的元素进行跳过
337+
if i > 0 && nums[i] == nums[i-1] && used[i-1] == false {
338+
continue
339+
}
340+
path = append(path, nums[i])
341+
used[i] = true
342+
backtracing(nums, i + 1, used)
343+
path = path[:len(path)-1]
344+
used[i] = false
345+
}
346+
}
347+
```
348+
349+
不使用used数组
313350
```Go
314351
var (
315352
path []int

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /