forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
24 lines (22 loc) · 783 Bytes
/
Solution.cs
File metadata and controls
24 lines (22 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace LeetCodeNet.G0001_0100.S0077_combinations {
// #Medium #Backtracking #Algorithm_I_Day_11_Recursion_Backtracking #Top_Interview_150_Backtracking
// #2025_07_04_Time_29_ms_(89.17%)_Space_97.46_MB_(59.96%)
public class Solution {
public IList<IList<int>> Combine(int n, int k) {
var res = new List<IList<int>>();
Backtrack(1, n, k, new List<int>(), res);
return res;
}
private void Backtrack(int start, int n, int k, List<int> curr, List<IList<int>> res) {
if (curr.Count == k) {
res.Add(new List<int>(curr));
return;
}
for (int i = start; i <= n; i++) {
curr.Add(i);
Backtrack(i + 1, n, k, curr, res);
curr.RemoveAt(curr.Count - 1);
}
}
}
}