首页 > 其他 > 详细

131. 分割回文串 回溯

时间:2020-07-07 13:24:14      阅读:51      评论:0      收藏:0      [点我收藏+]

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:

输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]


链接:https://leetcode-cn.com/problems/palindrome-partitioning

class Solution:
    def partition(self, s: str) -> List[List[str]]:
        res=[]
        def helper(s,tmp):
            if not s:
                res.append(tmp)//空字符说明处理完毕
            for i in range(1,len(s)+1):
                if s[:i]==s[:i][::-1]://判断回文
                    helper(s[i:],tmp+[s[:i]])//回溯
        
        helper(s,[])
        return res

 

131. 分割回文串 回溯

原文:https://www.cnblogs.com/xxxsans/p/13260104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!