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 ...
随机推荐
- [置顶] 单片机C语言易错知识点经验笔记
今天写这一篇文章并不是因为已经想好了一篇文章才写下来,而是我要将这一篇文章作为一个长期的笔记来写,我会一直更新.在进行单片机开发时,经常都会出现一些很不起眼的问题,这些问题其实都是很基础的c语言知识点 ...
- SQL - 删掉数据库
ALTER DATABASE [DB_NAME]SET OFFLINEWITH ROLLBACK IMMEDIATEGODROP DATABASE [DB_NAME]GO
- (转)iOS7界面设计规范(3) - UI基础 - 启动与退出
周二晚间来第三发,搞得好像今天是周六的赶脚.发掉之后再奖励自己一点冰啤酒吧,然后扑床去.天气热起来了,各位注意防暑降温呗.走起. 重要:这是针对于正在开发中的API或技术的预备文档(预发布版本).虽然 ...
- 基于drools创建自己的关系操作符
我们知道drools提供了12种关系操作符 但是有些时候这12种操作符依然不能满足我们的业务需求,我们可以扩展自己的操作符,下面是为某一航空公司做项目时扩展了操作符,在这分享下 首先,我们要实现的逻辑 ...
- [IOI1999]花店橱窗布置(DP路径记录)
题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...
- (转).net控件dropdownlist动态绑定数据
DropDownList控件的使用(数据绑定)(.net学习笔记二)(2006-10-12 07:28:49) 转载 分类:.net学习笔记 一.在页面初始化时候将集合绑定到DropDownLis ...
- linux删除ORACLE【weber出品必属精品】
关闭数据库 sqlplus / as sysdba shutdown abort 清除oracle软件 su - oracle cd $ORACLE_BASE rm -rf * rm -rf /etc ...
- NSSet使用小结
http://blog.csdn.net/ms2146/article/details/8657011
- 2、shell命令学习
1.第一个例子 touch test.sh vim test.sh #!/bin/bash echo "hello world" chmod 755 test.sh ./test. ...
- String.equals()
名称 说明 String.Equals(Obejecct) 确定String实例是否指 ...