【Lintcode】136.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.
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的更多相关文章
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【动态规划】POJ3280- Cheapest Palindrome
[题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j, ...
- 【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. ...
随机推荐
- BigDecimal的String类型
java本身对浮点型的计算会丢失精度,这个一定要注意,必须要用BigDecimal的String类型才能解决精度的问题. BigDecimal一共有四个构造方法: 我们在计算商品价格的时候,一定要用B ...
- Msfvenom 学习笔记与总结
平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...
- 在Editplus中配置java的(带包)编译(javac)和执行(java)的方法
配置的前提是电脑安装了JDK而且配置好了相关的环境变量(JAVA_HOME,path和classpath). 配置好后在命令行中输入javac和java验证是否配置成功: 假设出现上面的情况则说明配置 ...
- 关于一致/非一致代码段与TSS 关系的个人看法
[0]概念定义 0.1)一致代码段: 简单理解,就是操作系统拿出来被共享的代码段,可以被低特权级的用户直接调用访问的代码, 但是特权级高的程序不允许访问特权级低的数据. 通常这些共享代码,是" ...
- C#使用for循环移除HTML标记
public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...
- CI去掉 URL 中的 index.php
首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite 查找httpd.conf中是否开启了mod_rewrite.so 然后,在 CI 根目录下新建立一个配置文件,命名为 ...
- NuGet管理工具安装
安装完成后VS重启即可
- IOS开发之----异常处理
本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_ ...
- MongoDB的对象的创建
package com.voice.db; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mon ...
- 九度OJ 1179:阶乘 (循环)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5149 解决:1523 题目描述: 输入n, 求y1=1!+3!+...m!(m是小于等于n的最大奇数) y2=2!+4!+...p!(p是 ...