Palindrome Partitioning 解答
Question
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"]
]
Solution
基本思路还是递归。每次只探究第一刀切在哪儿。这里,为了避免重复计算,我们用DP来先处理子串是否对称的问题。思路参见 Palindrom Subarrays
如果想进一步节省时间,可以参见Word Break II的解法,将每个子问题的解存起来。
class Solution(object):
def construction(self, s):
length = len(s)
self.dp = [[False for i in range(length)] for j in range(length)]
for i in range(length):
self.dp[i][i] = True
for i in range(length - 1):
if s[i] == s[i + 1]:
self.dp[i][i + 1] = True
for sub_len in range(3, length + 1):
for start in range(0, length - sub_len + 1):
end = start + sub_len - 1
if s[start] == s[end] and self.dp[start + 1][end - 1]:
self.dp[start][end] = True def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.construction(s)
result = []
self.helper(s, 0, [], result)
return result def helper(self, s, start, cur_list, result):
length = len(s)
if start == length:
result.append(list(cur_list))
return
for end in range(start, length):
if self.dp[start][end]:
cur_list.append(s[start : end + 1])
self.helper(s, end + 1, cur_list, result)
cur_list.pop()
由于Python本身对字符串的强大处理,这道题的解答也可以为:
(比上一个解法花时间多)
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
return [[s[:i]] + rest
for i in xrange(1, len(s)+1)
if s[:i] == s[i-1::-1]
for rest in self.partition(s[i:])] or [[]]
Palindrome Partitioning 解答的更多相关文章
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LintCode Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Palindrome Partitioning II Leetcode
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- sgu495:概率dp / 推公式
概率题..可以dp也可以推公式 抽象出来的题目大意: 有 n个小球,有放回的取m次 问 被取出来过的小球的个数的期望 dp维护两个状态 第 i 次取出的是 没有被取出来过的小球的 概率dp[i] 和 ...
- 全球扫货指南:面向中国消费者的全球奢侈品旅游导购服务 |PingWest中文网
全球扫货指南:面向中国消费者的全球奢侈品旅游导购服务 |PingWest中文网 用"全球扫货指南
- SelectSort 选择排序
//SelectSort (( O(n²))) public class TestSelectSort { public int[] selectSortArray(int[] arr){ int m ...
- WPF - 使用WPF创建图表
最近有点想把自己的项目里面加入图表,让程序看起来高大上起来.没办法,很大一部分要靠包装,使用好图表,让程序图文并茂,就是包装的一个好法子.. WPF toolkit里面有常见的图表控件 如何使用: h ...
- javascript对象的理解
从代码中体会javascript中的对象: <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- Swift 2.0初探:值得注意的新特性
转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度的WWDC大会如期而至,在大会上A ...
- iOS会议和组织
全世界有许多iOS会议和组织,如果你没有机会去参加,知道他们的存在和向他们学习对你也是有益的.事实上,他们中有些提供免费的幻灯片.视频,有用资料等,所以你不能够忽视他们. 有一些会议的主题并不仅仅关于 ...
- node-sqlite3-API-归纳总结
SQLITE3-API-LIST:API1. new sqlite3.Database(filename,[mode],[callback]) 返回数据库对象并且自动打开和连接数据库 它没有独立打开数 ...
- struts 标签引用出错
几句句话概括 1.检查 web.xml 出现错误自己改 配置后filter jsp-config 2.检查 tld 目录下的东西 是否缺少 3. 将包复制到web的lib目录下后 之后 b ...
- hdu4355 三分
F - 三分 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit I ...