From 8de2e21d59d9fe68a9ac237affecf07b780c103f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 11 Dec 2023 09:35:55 +0800 Subject: [PATCH] =?UTF-8?q?Update0077.=E7=BB=84=E5=90=88=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 54 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index a7f00ffe..8bca6f24 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -792,7 +792,59 @@ def backtracking(result, path, n, j, k) end ``` - +### C# +```C# +// 暴力 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> 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(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> res = new List>(); + public IList path = new List(); + public IList> 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(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); + } + } +} +```