From 45863868708ba1c3260060fb5afa854fa6874184 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 21:03:46 -0500 Subject: [PATCH] =?UTF-8?q?Update=200131.=E5=88=86=E5=89=B2=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 143 ++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 52 deletions(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 30bba455..53f43eea 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -349,12 +349,9 @@ class Solution { ``` ## Python -**回溯+正反序判断回文串** +回溯 基本版 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] def partition(self, s: str) -> List[List[str]]: ''' @@ -363,52 +360,14 @@ class Solution: 当切割线迭代至字符串末尾,说明找到一种方法 类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线) ''' - self.path.clear() - self.paths.clear() - self.backtracking(s, 0) - return self.paths + result = [] + self.backtracking(s, 0, [], result) + return result - def backtracking(self, s: str, start_index: int) -> None: + def backtracking(self, s, start_index, path, result ): # Base Case - if start_index >= len(s): - self.paths.append(self.path[:]) - return - - # 单层递归逻辑 - for i in range(start_index, len(s)): - # 此次比其他组合题目多了一步判断: - # 判断被截取的这一段子串([start_index, i])是否为回文串 - temp = s[start_index:i+1] - if temp == temp[::-1]: # 若反序和正序相同,意味着这是回文串 - self.path.append(temp) - self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 - self.path.pop() - else: - continue -``` -**回溯+函数判断回文串** -```python -class Solution: - def __init__(self): - self.paths = [] - self.path = [] - - def partition(self, s: str) -> List[List[str]]: - ''' - 递归用于纵向遍历 - for循环用于横向遍历 - 当切割线迭代至字符串末尾,说明找到一种方法 - 类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线) - ''' - self.path.clear() - self.paths.clear() - self.backtracking(s, 0) - return self.paths - - def backtracking(self, s: str, start_index: int) -> None: - # Base Case - if start_index >= len(s): - self.paths.append(self.path[:]) + if start_index == len(s): + result.append(path[:]) return # 单层递归逻辑 @@ -416,9 +375,9 @@ class Solution: # 此次比其他组合题目多了一步判断: # 判断被截取的这一段子串([start_index, i])是否为回文串 if self.is_palindrome(s, start_index, i): - self.path.append(s[start_index:i+1]) - self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 - self.path.pop() # 回溯 + path.append(s[start_index:i+1]) + self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 + path.pop() # 回溯 else: continue @@ -430,9 +389,89 @@ class Solution: return False i += 1 j -= 1 - return True + return True ``` +回溯+优化判定回文函数 +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + self.backtracking(s, 0, [], result) + return result + + def backtracking(self, s, start_index, path, result ): + # Base Case + if start_index == len(s): + result.append(path[:]) + return + + # 单层递归逻辑 + for i in range(start_index, len(s)): + # 若反序和正序相同,意味着这是回文串 + if s[start_index: i + 1] == s[start_index: i + 1][::-1]: + path.append(s[start_index:i+1]) + self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 + path.pop() # 回溯 + else: + continue +``` +回溯+高效判断回文子串 +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + isPalindrome = [[False] * len(s) for _ in range(len(s))] # 初始化isPalindrome矩阵 + self.computePalindrome(s, isPalindrome) + self.backtracking(s, 0, [], result, isPalindrome) + return result + + def backtracking(self, s, startIndex, path, result, isPalindrome): + if startIndex >= len(s): + result.append(path[:]) + return + + for i in range(startIndex, len(s)): + if isPalindrome[startIndex][i]: # 是回文子串 + substring = s[startIndex:i + 1] + path.append(substring) + self.backtracking(s, i + 1, path, result, isPalindrome) # 寻找i+1为起始位置的子串 + path.pop() # 回溯过程,弹出本次已经填在的子串 + + def computePalindrome(self, s, isPalindrome): + for i in range(len(s) - 1, -1, -1): # 需要倒序计算,保证在i行时,i+1行已经计算好了 + for j in range(i, len(s)): + if j == i: + isPalindrome[i][j] = True + elif j - i == 1: + isPalindrome[i][j] = (s[i] == s[j]) + else: + isPalindrome[i][j] = (s[i] == s[j] and isPalindrome[i+1][j-1]) +``` +回溯+使用all函数判断回文子串 +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + self.partition_helper(s, 0, [], result) + return result + + def partition_helper(self, s, start_index, path, result): + if start_index == len(s): + result.append(path[:]) + return + + for i in range(start_index + 1, len(s) + 1): + sub = s[start_index:i] + if self.isPalindrome(sub): + path.append(sub) + self.partition_helper(s, i, path, result) + path.pop() + + def isPalindrome(self, s): + return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2)) + +``` ## Go ```go var (