题目:

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

Example

Given s = "aab", return:

[
["aa","b"],
["a","a","b"]
]

题解:

Solution 1 ()

class Solution {
public:
vector<vector<string>> partition(string s) {
if (s.empty()) {
return {{}};
}
vector<vector<string> > res;
vector<string> v;
dfs(res, v, s, ); return res;
}
void dfs(vector<vector<string> > &res, vector<string> &v, string s, int pos) {
if (pos >= s.size()) {
res.push_back(v);
return;
}
string str;
for (int i = pos; i < s.size(); ++i) {
str += s[i];
if (isValid(str)) {
v.push_back(str);
dfs(res, v, s, i + );
v.pop_back();
}
}
}
bool isValid(string str) {
if (str.empty()) {
return true;
}
int begin = ;
int end = str.size() - ;
for (; begin <= end; ++begin, --end) {
if (str[begin] != str[end]) {
return false;
}
}
return true;
}
};

Solution 1.2 ()

class Solution {
public:
bool isPalindrome(string &s, int i, int j){
while(i < j && s[i] == s[j]){
i++;
j--;
}
if(i >= j) {
return true;
} else {
return false;
}
}
void helper(vector<vector<string>> & res, vector<string> &cur, string &s, int pos){
if(pos == s.size()){
res.push_back(cur);
return;
}
for(int i = pos; i <= s.size() - ; i++){
if(isPalindrome(s, pos, i)){
cur.push_back(s.substr(pos, i - pos + ));
helper(res, cur, s, i+);
cur.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> cur;
helper(res, cur, s, ); return res;
}
};

【Lintcode】136.Palindrome Partitioning的更多相关文章

  1. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  2. 【LeetCode】131. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  3. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  4. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  5. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  6. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  7. 【动态规划】POJ3280- Cheapest Palindrome

    [题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j, ...

  8. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. PHP RSA加解密示例(转)

    1.生成密钥和公钥 开始前需要准备openssl环境 linux 需要安装openssl工具包,传送门http://www.openssl.org/source/ window 下需要安装openss ...

  2. 显存不够----ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[4096]

    ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[4096] 类似问题 h ...

  3. Multicast注册中心

    1 2 3 4 提供方启动时广播自己的地址. 消费方启动时广播订阅请求. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者. 消费方收到提供方地址时, ...

  4. 04 redis list结构及命令详解

    一:link 链表结构 lpush key value 作用: 把值插入到链接头部[右边] 注意:rpush key value 插入到左边 rpop key 作用: 返回并删除链表尾元素 rpush ...

  5. liunx 安装工具总结

    1  下载相关文件,比如hadoop 2  解压文件 tar -zxcf xxx.tar.gz 3  mv xxx 到指定目录:通常安装到/usr/local 或者自己建个目录 /usr/develo ...

  6. 用Delphi实现网络视频编程

    在MSN.QQ等聊天类的应用程序中,都应用到了网络视频技术.Delphi使用Object Pascal语言是一种完全面向对象语言,可以开发出灵活强大的程序,开发网络视频程序也不在话下.一个完整的网络视 ...

  7. Linux C语言 网络编程(二) server模型

    前面介绍了关于连接linux服务端方式,可是服务端的资源是有限的,所以我们通常须要又一次思考,设计一套server模型来处理相应的client的请求. 第一种:并发server.通过主进程统一处理cl ...

  8. (转)php 根据url自动生成缩略图并处理高并发问题

    分享是一种精神,与技术高低无关!   图片缩略图动态生成- [代码编程] 2011-08-23 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.c ...

  9. SAP 第四代增强-BTE

    第四代BTE实例详解:http://blog.csdn.net/wbin9752/article/details/7954922 第三代增强(BADI实例详解) :http://blog.csdn.n ...

  10. 通过JMX获取weblogic的监控指标

    通过JMX获取weblogic的监控数据,包括JDBC,SESSION,SERVERLET,JVM等信息.主要用到weblogic自己的t3协议,所以要用到weblogic的jar包:wlfullcl ...