【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. ...
随机推荐
- python2 和 python3兼容写法
一:使用ImportError,Python3中将一些Python2的模块名称做了修改,需要我们做一些处理来保证代码在不同Python版本中能够正常运行 # -*- coding: utf- -*- ...
- OpenCV学习(35) OpenCV中的PCA算法
PCA算法的基本原理可以参考:http://www.cnblogs.com/mikewolf2002/p/3429711.html 对一副宽p.高q的二维灰度图,要完整表示该图像,需要m = ...
- DeDeCMS(织梦)变量覆盖0day getshell
测试方法: @Sebug.net dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! #!usr/bin/php -w <?php error_reporting( ...
- python环境搭建-Pycharm模块安装方法
不懂直接看图顺序操作: 方法一: 方法二:
- matlab中,怎样把矩阵中所有的0改为2
一句话搞定:>> a(find(a==0))=[2]:把矩阵中所有的0改为2
- 重新安装 RCU-数据库 2014-11-22
删除数据库Endv(原RCU数据库) 重建数据库为LLS(新RCU数据库)..略.. Database Control URL 为 https://www:1158/em 管理资料档案库已置于安全模式 ...
- 使用(function() {}).call(this);包裹代码有什么好处,什么时候应该这样做?
转自:http://segmentfault.com/q/1010000002519489 1.严格模式下函数调用的 this 并不会默认成为全局对象. 使用 func.call(this) 确保函数 ...
- C#.NET常见问题(FAQ)-delegate委托链如何使用
委托链本质就是你把一堆要执行的东西放到一个list里面,当要触发一组事情的时候,就不需要一个一个写一遍了(比如厂里食堂开饭了,这个方法一执行,要让厨师A时间在食堂等候打饭,B类员工在某个时间排队打饭, ...
- STL - 容器 - 运行期指定排序准则
RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...
- STL - 容器 - Array
Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include <array> #include <algorithm> #include < ...