From 2cd982fd0dd478fb20989ee2dd8c1d28377f5829 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 15 Dec 2023 10:36:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update0040.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8C2=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/0040.组合总和II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 33e4a46f..994b04b8 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -773,7 +773,39 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public List> res = new List>(); + public List path = new List(); + public IList> CombinationSum2(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++) + { + if (i > start && candidates[i] == candidates[i - 1]) continue; + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i + 1, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} +```

From 69a38ff1d30e4eb245f6dcebf5c5d177b08e3ea4 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 16 Dec 2023 09:26:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update0131.=E5=88=86=E5=89=B2=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 80ac2843..1d230ca8 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -847,6 +847,50 @@ object Solution { } } ``` +### CSharp +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> Partition(string s) + { + BackTracking(s, 0); + return res; + } + public void BackTracking(string s, int start) + { + if (start >= s.Length) + { + res.Add(new List(path)); + return; + } + + for (int i = start; i < s.Length; i++) + { + if (IsPalindrome(s, start, i)) + { + path.Add(s.Substring(start, i - start + 1)); + } + else + { + continue; + } + BackTracking(s, i + 1); + path.RemoveAt(path.Count - 1); + } + } + public bool IsPalindrome(string s, int start, int end) + { + for (int i = start, j = end; i < j; i++, j--) + { + if (s[i] != s[j]) + return false; + } + return true; + } +} +```

From 32843da4dc1ed914ff5e030e1c09fd4d76b0ff73 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 17 Dec 2023 09:49:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update0093.=E5=A4=8D=E5=8E=9FIP=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=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/0093.复原IP地址.md | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 59cd92da..c662957a 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -799,6 +799,53 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList res = new List(); + public IList RestoreIpAddresses(string s) + { + if (s.Length < 4 || s.Length > 12) return res; + BackTracking(s, 0, 0); + return res; + } + public void BackTracking(string s, int start, int pointSum) + { + if (pointSum == 3) + { + if (IsValid(s, start, s.Length - 1)) + { + res.Add(s); + } + return; + } + for (int i = start; i < s.Length; i++) + { + if (IsValid(s, start, i)) + { + s = s.Insert(i + 1, "."); + BackTracking(s, i + 2, pointSum + 1); + s = s.Remove(i + 1, 1); + } + else break; + } + } + public bool IsValid(string s, int start, int end) + { + if (start > end) return false; + if (s[start] == '0' && start != end) return false; + int num = 0; + for (int i = start; i <= end; i++) + { + if (s[i] > '9' || s[i] < '0') return false; + num = num * 10 + s[i] - '0'; + if (num > 255) return false; + } + return true; + } +} +```