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 ...
随机推荐
- 玩转指针(Playing with Pointers)
Question: What is a Pointer? What are its limitations? What are its benefits? How do we use it? What ...
- 关于时间的操作(JavaScript版)——依据不同区时显示对应的时间
如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块--依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下: <!DOCTYPE HTML ...
- Python一日一练05----怒刷点击量
功能 自己主动获取CSDN文章列表,并对每篇文章添加点击量. 源代码 import urllib.request import re import time import random from bs ...
- CentOS6.6(单用户模式)重设root密码
1.开机时手要快按任意键,因为默认时间5s 2.grub菜单,只有一个内核,没什么好上下选的,按e键.不过如果你升级了系统或安装了Xen虚拟化后,就会有多个显示了. 3.接下来显示如下,选择第二项,按 ...
- AngularJs练习Demo12Provider
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- OpenGL ES 2.0 剪裁测试
剪裁测试:可以在渲染时用来限制绘制区域,通过此技术可以在屏幕(帧缓冲)上指定一个矩形区域. //启用剪裁测试 GLES20.glEnable(GL10.GL_SCISSOR_TEST); //设置区域 ...
- UVA10305 拓扑排序
网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...
- HDU 1269 裸奔的强联通分量
看了别人博客 http://blog.csdn.net/jokes000/article/details/7538994 #include <cstdio> #include <c ...
- Windows单击右键没有共享选项怎么办
文件共享是指在网络环境下文件.文件夹.某个硬盘分区使用时的一种设置属性,一般指多个用户可以同时打开或使用同一个文件或数据.但有时候也会遇到找不到共享选项的情况. Windows单击右键没有共享选项怎么 ...
- 扩展vbox硬盘大小
1.使用VboxManage list hdds查看所有安装的虚拟机信息 2.找到你要扩展硬盘容量虚拟机的UUID,使用modifyhd命令扩展硬盘大小 VBoxManage modifyhd U ...