【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. ...
随机推荐
- With Visual Studio, Open Same File In Two Windows, Updates Reflected in Both
I’ve always been frustrated in Visual Studio (all versions I can remember including latest vs2012) w ...
- 最小均方算法(LMS Algorithm)理论及DSP实现
LMS算法可认为是机器学习里面最基本也比较有用的算法,神经网络中对参数的学习使用的就是LMS的思想,在通信信号处理领域LMS也非常常见,比如自适应滤波器. 本文主要对LMS(Least Mean Sq ...
- go语言基础之随机数的使用
1.随机数的使用 示例1: 如果种子参数一样,每次运行程序产生的随机数都一样 package main //必须有个main包 import "fmt" import &quo ...
- Cesium随笔(3)随鼠标实时显示经纬度坐标以及高度【转】
在网页三维地球上进行可视化开发与经纬度坐标以及高度是分不开的,能够实时获取鼠标位置的经纬度对可视化效果有很好的帮助,Cesium当然能做到: (1)首先在里创建显示坐标的容器 样式自己调整的合适即可 ...
- 超级简单的jquery操作表格(添加/删除行、添加/删除列)
利用jquery给指定的table添加一行.删除一行 <script language="javascript" src="./jquery.js"> ...
- CentOS7.0 x86_64系统上构建php开发环境--Lamp(包含设置虚拟文件夹,加入SELinux对httpd的支持等知识)
一.安装mysql,直接用yum安装就可以,mysql在centos7.0版本号中被mariadb替代了. 命令: yum install mysql-server mysql 安装好了,选择改动my ...
- RAID5工作原理介绍
RAID 5是一种存储性能.数据安全和存储成本兼顾的存储解决方案.以四个硬盘组成的RAID 5为例,其数据存储方式如图4所示:图中,P0为D0,D1和D2的奇偶校验信息,P1为D3,D4,D5的奇偶校 ...
- poj_1681_高斯消元
这道题和之前的把那一道1222很类似.仅仅只是一定要注意一下对于无解的推断. /*########################################################### ...
- gravatar全球通用头像设定
一:说明: gravatar的头像设定,可以用于wordpress,github等社区: 一次设定,全球同步显示: 目前gravatar已不支持注册,需要注册wordpress.com,然后登录: w ...
- Oracle多行记录合并自定义函数
在oracle数据库中,进行字段合并,可以使用wm_concat(column)函数,但是在这种方法不被Oracle所推荐,因为WMSYS用户用于Workspace Manager,其函数对象可能因版 ...