-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path90-Subsets-II.swift
43 lines (35 loc) · 1.02 KB
/
90-Subsets-II.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// 90-Subsets II.swift
//
//
// Created by Lugick Wang on 2021/1/23.
//
import Foundation
class Solution {
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
var results = [[Int]]()
let candidates = nums.sorted()
var temp = [Int]()
backtrack(&results, temp: &temp, candidates: candidates, start: 0)
return results
}
func backtrack(_ results:inout [[Int]], temp:inout [Int], candidates:[Int], start: Int) {
results.append(temp)
print("results")
print(results)
for i in start..<candidates.count {
if i > start && candidates[i] == candidates[i-1] {
continue
}
temp.append(candidates[i])
print("i")
print(i)
print(candidates[i])
backtrack(&results, temp: &temp, candidates: candidates, start: i+1)
print("后退")
print(temp)
temp.removeLast()
print(temp)
}
}
}