【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning
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"]
]
空间换时间。
使用二维数组isPalin记录每个子串是否为回文串。
然后递归来做。可以看做深度优先搜索。
class Solution {
public:
vector<vector<bool> > isPalin; // isPalin[i][j]==true means s[i,...,j] is palindrome
void buildMap(string s)
{
int n = s.size();
isPalin.resize(n, vector<bool>(n, false));
for(int i = ; i < n; i ++)
isPalin[i][i] = true;
for(int i = n-; i >= ; i --)
{
for(int j = i+; j < n; j ++)
{
if(s[i] == s[j])
{
if(j == i+ || isPalin[i+][j-] == true)
isPalin[i][j] = true;
}
}
}
}
vector<vector<string>> partition(string s) {
buildMap(s);
vector<vector<string> > ret;
vector<string> cur;
Helper(ret, cur, s, );
return ret;
}
void Helper(vector<vector<string> >& ret, vector<string> cur, string s, int offset)
{
if(s == "")
ret.push_back(cur);
for(int i = ; i < s.size(); i ++)
{
if(isPalin[offset+][offset+i] == true)
{
cur.push_back(s.substr(, i+)); //palin prefix
Helper(ret, cur, s.substr(i+), offset+i+);
cur.pop_back();
}
}
}
};

【LeetCode】131. Palindrome Partitioning的更多相关文章
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【Lintcode】136.Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- coursera课程Text Retrieval and Search Engines之Week 3 Overview
Week 3 OverviewHelp Center Week 3 On this page: Instructional Activities Time Goals and Objectives K ...
- 学习笔记:A*算法
简易地图 如图所示简易地图, 其中绿色方块的是起点 (用 A 表示), 中间蓝色的是障碍物, 红色的方块 (用 B 表示) 是目的地. 为了可以用一个二维数组来表示地图, 我们将地图划分成一个个的小方 ...
- CSS结构和层叠
每个合法的文档都会生成一个文档树,从而能根据元素的祖先,属性,兄弟元素等创建选择器来选择元素.有了这个结构树,选择器才能起作用,这也是CSS继承的核心.继承是从一个元素向其后代元素传递属性值所采用的机 ...
- 【Qt】仿QQ表情选择控件
表情选择控件在聊天应用中常常要用到,做起来尽管不复杂可是非常繁琐.特别是有些图标须要按顺序排列.每次重做必定是非常费时.所以我将聊天表情选择控件封装成一个独立的类QFaceSelectWid ...
- HDU 2825 Wireless Password【AC自动机+DP】
给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...
- Tomcat gzip果然强大,js文件压缩率50%以上
Tomcat配置使用gzip,在server.xml中 <Connector port="9098" protocol="HTTP/1.1" connec ...
- 2017.8.30 elasticsearch-sql的安装与使用
参考来自: http://blog.csdn.net/u012307002/article/details/52837756 https://github.com/NLPchina/elasticse ...
- 【精】C语言之变量存储类型
- 内有干货!2个人3个月怎样从零完毕一款社区App《林卡》
嘿,大家好.我是不灭的小灯灯,我赌5毛你没听说过我的名字... 好啦.这篇不是鸡汤,是经验吐槽.干货分享! 所以乱七八糟的就不多说了.直接切入正题. 先说下自己的情况背景,眼下尚未毕业.非计算机专业, ...
- 安装--->Tomcat监控工具Probe
1.Porbe介绍 psi-probe用于对Tomcat进行监控,比tomcat的manager强大很多. 2.下载 probe-2.3.3.zip 或者 probe.war 3.将下载好的war ...