Update 0131.分割回文串.md

添加 0131.分割回文串 python3版本
This commit is contained in:
jojoo15
2021-05-25 15:14:00 +02:00
committed by GitHub
parent fe0abe75fd
commit ed8ca0daae

View File

@ -292,7 +292,24 @@ class Solution {
```
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