Merge pull request #836 from casnz1601/patch-13

Update 0131.分割回文串.md
This commit is contained in:
程序员Carl
2021-10-14 09:44:57 +08:00
committed by GitHub

View File

@ -290,59 +290,92 @@ class Solution {
``` ```
## Python ## Python
```python **回溯+正反序判断回文串**
# 版本一 ```python3
class Solution: class Solution:
def __init__(self):
self.paths = []
self.path = []
def partition(self, s: str) -> List[List[str]]: def partition(self, s: str) -> List[List[str]]:
res = [] '''
path = [] #放已经回文的子串 递归用于纵向遍历
def backtrack(s,startIndex): for循环用于横向遍历
if startIndex >= len(s): #如果起始位置已经大于s的大小说明已经找到了一组分割方案了 当切割线迭代至字符串末尾,说明找到一种方法
return res.append(path[:]) 类似组合问题为了不重复切割同一位置需要start_index来做标记下一轮递归的起始位置(切割线)
for i in range(startIndex,len(s)): '''
p = s[startIndex:i+1] #获取[startIndex,i+1]在s中的子串 self.path.clear()
if p == p[::-1]: path.append(p) #是回文子串 self.paths.clear()
else: continue #不是回文,跳过 self.backtracking(s, 0)
backtrack(s,i+1) #寻找i+1为起始位置的子串 return self.paths
path.pop() #回溯过程弹出本次已经填在path的子串
backtrack(s,0) def backtracking(self, s: str, start_index: int) -> None:
return res # 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 **回溯+函数判断回文串**
# 版本二 ```python3
class Solution: class Solution:
def __init__(self):
self.paths = []
self.path = []
def partition(self, s: str) -> List[List[str]]: def partition(self, s: str) -> List[List[str]]:
res = [] '''
path = [] #放已经回文的子串 递归用于纵向遍历
# 双指针法判断是否是回文串 for循环用于横向遍历
def isPalindrome(s): 当切割线迭代至字符串末尾,说明找到一种方法
n = len(s) 类似组合问题为了不重复切割同一位置需要start_index来做标记下一轮递归的起始位置(切割线)
i, j = 0, n - 1 '''
while i < j: self.path.clear()
if s[i] != s[j]:return False self.paths.clear()
i += 1 self.backtracking(s, 0)
j -= 1 return self.paths
return True
def backtracking(self, s: str, start_index: int) -> None:
def backtrack(s, startIndex): # Base Case
if startIndex >= len(s): # 如果起始位置已经大于s的大小说明已经找到了一组分割方案了 if start_index >= len(s):
res.append(path[:]) self.paths.append(self.path[:])
return return
for i in range(startIndex, len(s)):
p = s[startIndex:i+1] # 获取[startIndex,i+1]在s中的子串 # 单层递归逻辑
if isPalindrome(p): # 是回文子串 for i in range(start_index, len(s)):
path.append(p) # 此次比其他组合题目多了一步判断:
else: continue #不是回文,跳过 # 判断被截取的这一段子串([start_index, i])是否为回文串
backtrack(s, i + 1) if self.is_palindrome(s, start_index, i):
path.pop() #回溯过程弹出本次已经填在path的子串 self.path.append(s[start_index:i+1])
backtrack(s, 0) self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串
return res self.path.pop() # 回溯
else:
continue
def is_palindrome(self, s: str, start: int, end: int) -> bool:
i: int = start
j: int = end
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
``` ```
## Go ## Go
**注意切片go切片是披着值类型外衣的引用类型**
注意切片go切片是披着值类型外衣的引用类型
```go ```go
func partition(s string) [][]string { func partition(s string) [][]string {
var tmpString []string//切割字符串集合 var tmpString []string//切割字符串集合