-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0078.swift
73 lines (70 loc) · 1.4 KB
/
q0078.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// q0078.swift
// LeetcodeSwift
//
// Created by NowOrNever on 10/03/2020.
// Copyright © 2020 DL. All rights reserved.
//
import Foundation
//78. Subsets
//Medium
//
//2959
//
//71
//
//Add to List
//
//Share
//Given a set of distinct integers, nums, return all possible subsets (the power set).
//
//Note: The solution set must not contain duplicate subsets.
//
//Example:
//
//Input: nums = [1,2,3]
//Output:
//[
// [3],
// [1],
// [2],
// [1,2,3],
// [1,3],
// [2,3],
// [1,2],
// []
//]
//Accepted
//491,010
//Submissions
//844,844
class q0078Solution {
func subsets_helper(_ nums: [Int], k: Int, start:Int, path: inout [Int], result:inout [[Int]]) {
if path.count == k {
result.append(path)
return
}
if path.count + nums.count - start < k {
return
}
for i in start..<nums.count {
path.append(nums[i])
subsets_helper(nums, k: k, start: i + 1, path: &path, result: &result)
path.removeLast()
}
}
func subsets(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
var path = [Int]()
for i in 0...nums.count {
subsets_helper(nums,k: i, start: 0, path: &path, result: &result)
}
return result
}
}
func q0078(){
let solu = q0078Solution()
let candidates = [1,2,3]
let result = solu.subsets(candidates)
print(result)
}