mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0131.分割回文串.md
This commit is contained in:
@ -349,12 +349,9 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
**回溯+正反序判断回文串**
|
回溯 基本版
|
||||||
```python
|
```python
|
||||||
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]]:
|
||||||
'''
|
'''
|
||||||
@ -363,52 +360,14 @@ class Solution:
|
|||||||
当切割线迭代至字符串末尾,说明找到一种方法
|
当切割线迭代至字符串末尾,说明找到一种方法
|
||||||
类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线)
|
类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线)
|
||||||
'''
|
'''
|
||||||
self.path.clear()
|
result = []
|
||||||
self.paths.clear()
|
self.backtracking(s, 0, [], result)
|
||||||
self.backtracking(s, 0)
|
return result
|
||||||
return self.paths
|
|
||||||
|
|
||||||
def backtracking(self, s: str, start_index: int) -> None:
|
def backtracking(self, s, start_index, path, result ):
|
||||||
# Base Case
|
# Base Case
|
||||||
if start_index >= len(s):
|
if start_index == len(s):
|
||||||
self.paths.append(self.path[:])
|
result.append(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[:])
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# 单层递归逻辑
|
# 单层递归逻辑
|
||||||
@ -416,9 +375,9 @@ class Solution:
|
|||||||
# 此次比其他组合题目多了一步判断:
|
# 此次比其他组合题目多了一步判断:
|
||||||
# 判断被截取的这一段子串([start_index, i])是否为回文串
|
# 判断被截取的这一段子串([start_index, i])是否为回文串
|
||||||
if self.is_palindrome(s, start_index, i):
|
if self.is_palindrome(s, start_index, i):
|
||||||
self.path.append(s[start_index:i+1])
|
path.append(s[start_index:i+1])
|
||||||
self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串
|
self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串
|
||||||
self.path.pop() # 回溯
|
path.pop() # 回溯
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -430,9 +389,89 @@ class Solution:
|
|||||||
return False
|
return False
|
||||||
i += 1
|
i += 1
|
||||||
j -= 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
|
||||||
```go
|
```go
|
||||||
var (
|
var (
|
||||||
|
Reference in New Issue
Block a user