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"]
]

判断回文,简单的入栈出栈判断,其他的就是简单的回溯了。

class Solution {
private:
vector<vector<string>> res;
vector<string> tempRes;
public:
bool isValid(string str)
{
stack<char> stk;
for(int i=;i<str.size();i++)
stk.push(str[i]);
for(int i=;i<str.size();i++ )
{
if(str[i]!=stk.top())
return false;
else
stk.pop();
}
return true;
}
void getRes(string str)
{
if(str.size()==)
{
res.push_back(tempRes);
return ;
}
string temp;
for(int i=;i<str.size();i++)
{
temp+=str[i];
if(isValid(temp))
{
tempRes.push_back(temp);
getRes(str.substr(i+));
tempRes.pop_back();
}
}
} vector<vector<string>> partition(string s) {
getRes(s);
return res;
}
};

Palindrome Partitioning——回溯算法的又一经典的更多相关文章

  1. 回溯算法之n皇后问题

    今天在看深度优先算法的时候,联想到DFS本质不就是一个递归回溯算法问题,只不过它是应用在图论上的.OK,写下这篇博文也是为了回顾一下回溯算法设计吧. 学习回溯算法问题,最为经典的问题我想应该就是八皇后 ...

  2. 算法刷题--回溯算法与N皇后

    所谓回溯算法,在笔者看来就是一种直接地思想----假设需要很多步操作才能求得最终的解,每一步操作又有很多种选择,那么我们就直接选择其中一种并依次深入下去.直到求得最终的结果,或是遇到明细的错误,回溯到 ...

  3. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  4. 【算法】leetcode之 Palindrome Partitioning I&II(转载)

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  5. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  6. LeetCode131:Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  7. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  8. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

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

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

随机推荐

  1. YBT 5.1 区间类动态规划

    题解在代码中 石子合并[loj 10147] /* dp[i][j]=max or min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]) i<=k& ...

  2. [zhuan]VMware中bridge方式网络不能上网的解决办法

    http://jingpin.jikexueyuan.com/article/31601.html 安装好VMware 7后,打开原来的虚拟机文件,发现不能上网,原来的Ethernet是设置的Brid ...

  3. 推荐一款JQuery星形评级插件

    jRating 是一个非常灵活的jQuery插件用于快速创建一个Ajax星型投票系统.可以设置星型数量和小数支持.功能很强大,具体大家可以看一下这个插件的js代码就知道了,下面这里演示一下这个插件有哪 ...

  4. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A B 水 搜索

    A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. P2075 [NOIP2012T5]借教室 区间更新+二分查找

    P2075 [NOIP2012T5]借教室 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 noip2012-tg 描述 在大学期间,经常需要租借教室.大到院 ...

  6. Codeforces Round #342 (Div. 2) B

    B. War of the Corporations time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. Codeforces Round #340 (Div. 2)B

    B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. HDU3265 线段树(扫描线)

    Posters Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. c# 定时执行任务

    在Global.asax文件中加上 void Application_Start(object sender, EventArgs e) { // Code that runs on applicat ...

  10. java RSA加密解密实现(含分段加密)

    该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1.jar 下载地址:http://download.csdn.net/detail/centralperk/50255 ...