From 0e55c1b424c4dac5cb78c304f412fce9fec14e47 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 9 Dec 2023 09:35:59 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Update0108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8.将有序数组转换为二叉搜索树.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index e699005e..4ccfc5f1 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -530,6 +530,23 @@ impl Solution { } } ``` +### C# +```csharp +// 递归 +public TreeNode SortedArrayToBST(int[] nums) +{ + return Traversal(nums, 0, nums.Length - 1); +} +public TreeNode Traversal(int[] nums, int left, int right) +{ + if (left > right) return null; + int mid = left + (right - left) / 2; + TreeNode node = new TreeNode(nums[mid]); + node.left = Traversal(nums, left, mid - 1); + node.right = Traversal(nums, mid + 1, right); + return node; +} +```

From e59dbae465c5247ff044a7d7fecd0b1cb6e22efe Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 10 Dec 2023 09:25:18 +0800 Subject: [PATCH 2/6] =?UTF-8?q?update538.=E6=8A=8A=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA=E7=B4=AF?= =?UTF-8?q?=E5=8A=A0=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0538.把二叉搜索树转换为累加树.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index c403c98f..07cae1ad 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -529,6 +529,23 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public class Solution +{ + int pre = 0; + public TreeNode ConvertBST(TreeNode root) + { + if (root == null) return null; + ConvertBST(root.right); + root.val += pre; + pre = root.val; + ConvertBST(root.left); + return root; + } +} +```

From 8de2e21d59d9fe68a9ac237affecf07b780c103f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 11 Dec 2023 09:35:55 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Update0077.=E7=BB=84=E5=90=88=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=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); + } + } +} +```

From d3e61f36abf8b40fa9afac4e7c414f13d8bfcec7 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 12 Dec 2023 09:20:04 +0800 Subject: [PATCH 4/6] =?UTF-8?q?Update0216.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8C3=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 61 +++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index c0cb8860..ac28f9fc 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -633,7 +633,66 @@ object Solution { } } ``` - +### C# +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum3(int k, int n) + { + BackTracking(k, n, 0, 1); + return res; + } + public void BackTracking(int k, int n, int sum, int start) + { + if (path.Count == k) + { + if (sum == n) + res.Add(new List(path)); + return; + } + for (int i = start; i <= 9; i++) + { + sum += i; + path.Add(i); + BackTracking(k, n, sum, i + 1); + sum -= i; + path.RemoveAt(path.Count - 1); + } + } +} +// 剪枝 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum3(int k, int n) + { + BackTracking(k, n, 0, 1); + return res; + } + public void BackTracking(int k, int n, int sum, int start) + { + if (sum > n) + return; + if (path.Count == k) + { + if (sum == n) + res.Add(new List(path)); + return; + } + for (int i = start; i <= 9 - (k - path.Count) + 1; i++) + { + sum += i; + path.Add(i); + BackTracking(k, n, sum, i + 1); + sum -= i; + path.RemoveAt(path.Count - 1); + } + } +} +``` From c14df2f5779314078744090b62d9745f34de1d24 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 13 Dec 2023 09:45:51 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Update0017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 90296efd..a77bce46 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -732,6 +732,38 @@ def backtracking(result, letter_map, digits, path, index) end end ``` +### C# +```C# +public class Solution +{ + public IList res = new List(); + public string s; + public string[] letterMap = new string[10] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; + public IList LetterCombinations(string digits) + { + if (digits.Length == 0) + return res; + BackTracking(digits, 0); + return res; + } + public void BackTracking(string digits, int index) + { + if (index == digits.Length) + { + res.Add(s); + return; + } + int digit = digits[index] - '0'; + string letters = letterMap[digit]; + for (int i = 0; i < letters.Length; i++) + { + s += letters[i]; + BackTracking(digits, index + 1); + s = s.Substring(0, s.Length - 1); + } + } +} +```

From dcc37da6034f3ad8166c9f06ee2ab75a3ad204e7 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 14 Dec 2023 10:01:34 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Update0039.=E7=BB=84=E5=90=88=E7=BB=BC?= =?UTF-8?q?=E5=90=88=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 37d5614e..81558cc1 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -598,6 +598,66 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum(int[] candidates, int target) + { + BackTracking(candidates, target, 0, 0); + return res; + } + public void BackTracking(int[] candidates, int target, int start, int sum) + { + if (sum > target) return; + if (sum == target) + { + res.Add(new List(path)); + return; + } + for (int i = start; i < candidates.Length; i++) + { + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} + +// 剪枝优化 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum(int[] candidates, int target) + { + Array.Sort(candidates); + BackTracking(candidates, target, 0, 0); + return res; + } + public void BackTracking(int[] candidates, int target, int start, int sum) + { + if (sum > target) return; + if (sum == target) + { + res.Add(new List(path)); + return; + } + for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++) + { + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} +```