-
Notifications
You must be signed in to change notification settings - Fork 0
/
78-Subsets.swift
39 lines (32 loc) · 932 Bytes
/
78-Subsets.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
//
// 78-Subsets.swift
//
//
// Created by Lugick Wang on 2021/1/19.
//
import Foundation
class Solution {
func subsets(_ 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 {
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)
}
}
}