mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Update 0131.分割回文串.md
添加 0131.分割回文串 python3版本
This commit is contained in:
@ -292,7 +292,24 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def partition(self, s: str) -> List[List[str]]:
|
||||||
|
res = []
|
||||||
|
path = [] #放已经回文的子串
|
||||||
|
def backtrack(s,startIndex):
|
||||||
|
if startIndex >= len(s): #如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
|
||||||
|
return res.append(path[:])
|
||||||
|
for i in range(startIndex,len(s)):
|
||||||
|
p = s[startIndex:i+1] #获取[startIndex,i+1]在s中的子串
|
||||||
|
if p == p[::-1]: path.append(p) #是回文子串
|
||||||
|
else: continue #不是回文,跳过
|
||||||
|
backtrack(s,i+1) #寻找i+1为起始位置的子串
|
||||||
|
path.pop() #回溯过程,弹出本次已经填在path的子串
|
||||||
|
backtrack(s,0)
|
||||||
|
return res
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user