首页 > 编程语言 > 详细

【算法题13 分割回文串】

时间:2018-06-03 20:47:15      阅读:294      评论:0      收藏:0      [点我收藏+]

来源LeetCode 131:

题目:


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

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

示例:

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

解题代码:

 1 class Solution:
 2     def partition(self, s):
 3         """
 4         :type s: str
 5         :rtype: List[List[str]]
 6         """
 7         res=[]
 8         self.dfs(s,[],res)
 9         return res
10     
11     def dfs(self,s,path,res):
12         if not s:
13             res.append(path)
14             return
15         for i in range(1,len(s)+1):
16             if s[:i]==s[i-1::-1]:
17                 self.dfs(s[i:],path+[s[:i]],res)
18                 

 

【算法题13 分割回文串】

原文:https://www.cnblogs.com/yanmk/p/9130297.html

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