Skip to content

Commit 20b0f51

Browse files
authoredAug 30, 2024
feat: add swift implementation to lcof2 problem: No.079 (#3458)
1 parent d90b1ed commit 20b0f51

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎lcof2/剑指 Offer II 079. 所有子集/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ impl Solution {
188188
}
189189
```
190190

191+
#### Swift
192+
193+
```swift
194+
class Solution {
195+
func subsets(_ nums: [Int]) -> [[Int]] {
196+
var res = [[Int]]()
197+
dfs(0, nums, [], &res)
198+
return res
199+
}
200+
201+
private func dfs(_ i: Int, _ nums: [Int], _ current: [Int], _ res: inout [[Int]]) {
202+
res.append(current)
203+
for j in i..<nums.count {
204+
var newCurrent = current
205+
newCurrent.append(nums[j])
206+
dfs(j + 1, nums, newCurrent, &res)
207+
}
208+
}
209+
}
210+
```
211+
191212
<!-- tabs:end -->
192213

193214
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func subsets(_ nums: [Int]) -> [[Int]] {
3+
var res = [[Int]]()
4+
dfs(0, nums, [], &res)
5+
return res
6+
}
7+
8+
private func dfs(_ i: Int, _ nums: [Int], _ current: [Int], _ res: inout [[Int]]) {
9+
res.append(current)
10+
for j in i..<nums.count {
11+
var newCurrent = current
12+
newCurrent.append(nums[j])
13+
dfs(j + 1, nums, newCurrent, &res)
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)