Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
Subscribe to see which companies asked this question
经典的回溯方法,回溯基本上就是这个方法,按照这个模子套用就行。
class Solution(object):
def bt(self,s,temp,start,l,res):
#print l,temp,'@@'
if l == len(s):
res.append(temp)
return
for i in range(start+1,len(s)+1):
t = s[start:i]
#print i,t
if t == t[::-1]:
self.bt(s,temp+[t],i,l+(i-start),res)
def partition(self, s):
res = []
self.bt(s,[],0,0,res)
#print res
return res

本文介绍了一种使用回溯法解决字符串回文串分割问题的方法,并提供了一个具体的Python实现示例。通过递归地检查子串是否为回文串来生成所有可能的分割方案。

497

被折叠的 条评论
为什么被折叠?



