File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff 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
314351var (
315352 path []int
You can’t perform that action at this time.
0 commit comments