Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8de2e21

Browse files
committed
Update0077.组合,添加C#
1 parent e59dbae commit 8de2e21

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

‎problems/0077.组合.md‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,59 @@ def backtracking(result, path, n, j, k)
792792
end
793793

794794
```
795-
795+
### C#
796+
```C#
797+
// 暴力
798+
public class Solution
799+
{
800+
public IList<IList<int>> res = new List<IList<int>>();
801+
public IList<int> path = new List<int>();
802+
public IList<IList<int>> Combine(int n, int k)
803+
{
804+
BackTracking(n, k, 1);
805+
return res;
806+
}
807+
public void BackTracking(int n, int k, int start)
808+
{
809+
if (path.Count == k)
810+
{
811+
res.Add(new List<int>(path));
812+
return;
813+
}
814+
for (int i = start; i <= n; i++)
815+
{
816+
path.Add(i);
817+
BackTracking(n, k, i + 1);
818+
path.RemoveAt(path.Count - 1);
819+
}
820+
}
821+
}
822+
// 剪枝
823+
public class Solution
824+
{
825+
public IList<IList<int>> res = new List<IList<int>>();
826+
public IList<int> path = new List<int>();
827+
public IList<IList<int>> Combine(int n, int k)
828+
{
829+
BackTracking(n, k, 1);
830+
return res;
831+
}
832+
public void BackTracking(int n, int k, int start)
833+
{
834+
if (path.Count == k)
835+
{
836+
res.Add(new List<int>(path));
837+
return;
838+
}
839+
for (int i = start; i <= n - (k - path.Count) + 1; i++)
840+
{
841+
path.Add(i);
842+
BackTracking(n, k, i + 1);
843+
path.RemoveAt(path.Count - 1);
844+
}
845+
}
846+
}
847+
```
796848
<p align="center">
797849
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
798850
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /