LeetCode Palindrome Partitioning
来源:互联网 发布:360水滴破解软件 编辑:程序博客网 时间:2024/06/05 11:06
LeetCode解题之Palindrome Partitioning
原题
将一个字符串分割成若干个子字符串,使得子字符串都是回文字符串,要求列出所有的分割方案。
注意点:
- 无
例子:
输入: s = “aab”
输出: result = [[“a”, “a”, “b”], [“aa”, “b”]]
解题思路
采用了最简单的递归方法,将一个字符串分为前后两部分,如果第一部分是一个回文字符串,则对第二部分再次分割,不断递归,直到递归的终止条件——字符串为空为止;如果第一部分不是一个回文字符串,则尝试下一种分割方法。
AC源码
class Solution(object): def partition(self, s): """ :type s: str :rtype: List[List[str]] """ if not s: return [[]] result = [] for i in range(len(s)): if self.isPalindrome(s[:i + 1]): for r in self.partition(s[i + 1:]): result.append([s[:i + 1]] + r) return result def isPalindrome(self, s): return s == s[::-1]if __name__ == "__main__": assert Solution().partition("aab") == [ ["a", "a", "b"], ["aa", "b"] ]
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。
1 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- Oil Deposits 深度优先搜素油田
- FragmentTabHost的应用
- LeetCode Product of Array Except Self
- java环境下利用blind xxe来列目录和文件读取
- JQuery获取select选中的值
- LeetCode Palindrome Partitioning
- 尼姆博弈(Nimm Game)
- windows下编译sba(sparse bundle adjustment)
- 【我的笔记】Mac电脑的一些操作
- Javascript数组方法(译)
- 使用Callable接口实现多线程
- Android中对SD卡的读取与写入
- TabSiPlus开源代码如何使用
- 《struts2》指定多个配置文件和默认Action