【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. ...
随机推荐
- MP4文件格式的解析,以及MP4文件的分割算法
http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...
- js运算【按位非】~
这个运算符有点意思:按位非[~] 先来几个例子: ~undefined: -1 ~false: -1 ~true: -2 ~10:-11 找出规律了吧~~ 再说一下运用场景: 之前判断字符串是否存在是 ...
- 最新精品 强势来袭 XP,32/64位Win7,32/64位Win10系统【电脑城版】
随着Windows 10Build 10074 Insider Preview版发布,有理由相信,Win10离最终RTM阶段已经不远了.看来稍早前传闻的合作伙伴透露微软将在7月底正式发布Win10的消 ...
- hdu 5538 House Building(长春现场赛——水题)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538 House Building Time Limit: 2000/1000 MS (Java/Others) ...
- npm 全局配置放在c盘/用户/当前用户/目录下
prefix=D:\Users\Ed\AppData\Roaming\nodejs\npm-globalcache=D:\Users\Ed\AppData\Roaming\npm-cacheregis ...
- Centos7重新安装yum
Centos7重新安装yum rpm -qa|grep yum 然后用下面的命令删除出现的xxx包: rpm -e --nodeps xxx 下载 python-urlgrabber-3.10-8.e ...
- WePY根据环境变量来改变运行时的参数
WePY根据环境变量来改变运行时的参数 · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/WePY%E6%A0%B9%E6%8D%AE% ...
- 一阶 斜率 二阶 原函数的粗糙度 roughness
1 2 损失函数+惩罚函数 2阶导数
- jxl java工具类,导出excel,导入数据库
1: 引入jxl jar 我使用的为maven管理, <!--Excel工具--> <dependency> <groupId>net.sourceforge.je ...
- 移动App该怎样保存用户password
版权声明:本文为横云断岭原创文章,未经博主同意不得转载.微信公众号:横云断岭的专栏 https://blog.csdn.net/hengyunabc/article/details/34623957 ...