From 69a38ff1d30e4eb245f6dcebf5c5d177b08e3ea4 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 16 Dec 2023 09:26:57 +0800 Subject: [PATCH] =?UTF-8?q?Update0131.=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87?= =?UTF-8?q?=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; + } +} +```