Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add swift implementation to lcof2 problem: No.080 #3459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lcof2/剑指 Offer II 080. 含有 k 个元素的组合/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ func dfs(i, n, k int, t []int, res *[][]int) {
}
```

#### Swift

```swift
class Solution {
func combine(_ n: Int, _ k: Int) -> [[Int]] {
var res = [[Int]]()
dfs(1, n, k, [], &res)
return res
}

private func dfs(_ start: Int, _ n: Int, _ k: Int, _ current: [Int], _ res: inout [[Int]]) {
if current.count == k {
res.append(current)
return
}

if start > n {
return
}

for i in start...n {
var newCurrent = current
newCurrent.append(i)
dfs(i + 1, n, k, newCurrent, &res)
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
func combine(_ n: Int, _ k: Int) -> [[Int]] {
var res = [[Int]]()
dfs(1, n, k, [], &res)
return res
}

private func dfs(_ start: Int, _ n: Int, _ k: Int, _ current: [Int], _ res: inout [[Int]]) {
if current.count == k {
res.append(current)
return
}

if start > n {
return
}

for i in start...n {
var newCurrent = current
newCurrent.append(i)
dfs(i + 1, n, k, newCurrent, &res)
}
}
}
Loading