题目链接:leetcode
思路:
回溯
class Solution:
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.s =s
def dfs(s,res,tmp):
if not s:
res.append(tmp[:])
return
for i in range(1,len(s)+1):
stmp = s[:i]
if stmp == stmp[::-1]:
#这行代码等价于
#tmp.append(stmp)
#dfs(s[i:],res,tmp)
#tmp.pop()
dfs(s[i:],res,tmp+[stmp])
res=[]
dfs(self.s,res,[])
return res