mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update0077.组合,添加C#
This commit is contained in:
@ -792,7 +792,59 @@ def backtracking(result, path, n, j, k)
|
||||
end
|
||||
|
||||
```
|
||||
|
||||
### C#
|
||||
```C#
|
||||
// 暴力
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> Combine(int n, int k)
|
||||
{
|
||||
BackTracking(n, k, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int n, int k, int start)
|
||||
{
|
||||
if (path.Count == k)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= n; i++)
|
||||
{
|
||||
path.Add(i);
|
||||
BackTracking(n, k, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 剪枝
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> Combine(int n, int k)
|
||||
{
|
||||
BackTracking(n, k, 1);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int n, int k, int start)
|
||||
{
|
||||
if (path.Count == k)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= n - (k - path.Count) + 1; i++)
|
||||
{
|
||||
path.Add(i);
|
||||
BackTracking(n, k, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user