http://yucoding.blogspot.com/2013/08/leetcode-question-132-palindrome.html

Analysis:
When face the "return all", "get all ", "find all possible", "find the total number of", an idea is to use the recursion. Same as this problem!

To get the all the partitions of a string s:
1. find all the palindromes in substring s[0], and all the palindromes in substring s[1:end]
2. find all the palindromes in substring s[0:1], and all the palindromes in substring s[2:end]
...
find all the palindromes in substring s[1:end-1], and all the palindromes in substring s[end]

So the problem is quite clear, when we do recursion, two things should be considered:
1. stop condition:  when the search goes to the last position in the string
2. for loop or while loop:   for position=current start position to the end.

This problem is not complex, see the code below and you will understand the idea:

Code:

 class Solution {
public: bool valid(string &str, int st, int ed){
while (st<ed){
if (str[ed]!=str[st]){
return false;
}else{
st++;
ed--;
}
}
return true;
} void find(string s, int st, vector<string> &r, vector<vector<string> > &res){
if (st>=s.size()){
res.push_back(r);
}else{
for (int i=st;i<s.size();i++){
if (valid(s,st,i)){
r.push_back(s.substr(st,i-st+));
find(s,i+,r,res);
r.pop_back();
} }
}
} vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string> > res;
vector<string> r;
find(s,,r,res);
return res;
}
};

======================================================

http://fisherlei.blogspot.com/2013/03/leetcode-palindrome-partitioning.html

[Thoughts]
这种需要输出所有结果的基本上都是DFS的解法。实现如下。

[Code]

1:       vector<vector<string>> partition(string s) {
2: vector<vector<string>> result;
3: vector<string> output;
4: DFS(s, 0, output, result);
5: return result;
6: }
7: void DFS(string &s, int start, vector<string>& output, vector<vector<string>> &result)
8: {
9: if(start == s.size())
10: {
11: result.push_back(output);
12: return;
13: }
14: for(int i = start; i< s.size(); i++)
15: {
16: if(isPalindrome(s, start, i))
17: {
18: output.push_back(s.substr(start, i-start+1));
19: DFS(s, i+1, output, result);
20: output.pop_back();
21: }
22: }
23: }
24: bool isPalindrome(string &s, int start, int end)
25: {
26: while(start< end)
27: {
28: if(s[start] != s[end])
29: return false;
30: start++; end--;
31: }
32: return true;
33: }

leetcode-palindrome partitioning-ZZ的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

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

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  4. [LeetCode] Palindrome Partitioning 拆分回文串

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

  5. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  6. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  7. LeetCode: Palindrome Partitioning 解题报告

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

  8. Leetcode: Palindrome Partitioning II

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

  9. [Leetcode] Palindrome Partitioning

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

  10. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

随机推荐

  1. vue 无限级分类导航

    递归组件,实现无限级分类导航 https://cn.vuejs.org/v2/guide/components-edge-cases.html#%E9%80%92%E5%BD%92%E7%BB%84% ...

  2. java se系列(十二)集合

    1.集合 1.1.什么是集合 存储对象的容器,面向对象语言对事物的体现,都是以对象的形式来体现的,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式.集合的出现就是为了持有对象.集 ...

  3. element ui 表格提交时获取所有选中的checkbox的数据

    <el-table ref="multipleTable" :data="appList" @selection-change="changeF ...

  4. 【转】常用算法复习及实现(C++版)

    一.霍夫曼树实现 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大 ...

  5. JAVA学习3:Eclipse中集成Tomcat

    问题: 很多时候在Eclipse中启动Tmocat后,不能访问本机的localhost:8080主页,并且其他项目也不能访问. 原因: 打开Tomcat下的webapp后也找补到项目目录,这是因为Ec ...

  6. python+requests抓取页面图片

    前言: 学完requests库后,想到可以利用python+requests爬取页面图片,想到实战一下.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图片,所以我选取饿 ...

  7. android 用命令行打包生成 apk

    android 用 ant 进行 build. android sdk 下面的 tools/ant 下面的 build.xml 就是 build 的整个过程. 其中的 build 参数可在 ant.p ...

  8. linux install oracle jdk

    1 到oracle 官方网站下载jdk1.7 2 然后mv到 /usr/local/目录下 2.1 path 下添加/usr/sbin/ 3 使用update-alternative用来对系统中不同版 ...

  9. git 学习之基本操作

    之前的帖子已经讲述了什么是 Git 的仓库,并且添加了文件到 Git 的仓库,这里我们来学习下一些简单的操作. status 和 diff  之前我们已经提交了了一个 testFile.txt 的文件 ...

  10. Hibernate 集合映射

    Set映射: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mappi ...