Skip to content

Commit

Permalink
Update0077.组合,添加C#
Browse files Browse the repository at this point in the history
  • Loading branch information
eeee0717 committed Dec 11, 2023
1 parent e59dbae commit 8de2e21
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion problems/0077.组合.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down

0 comments on commit 8de2e21

Please sign in to comment.