leetcode-palindrome partitioning-ZZ

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:

 1 class Solution {
 2 public:
 3 
 4     bool valid(string &str, int st, int ed){
 5         while (st<ed){
 6             if (str[ed]!=str[st]){
 7                 return false;
 8             }else{
 9                 st++;
10                 ed--;
11             }
12         }
13         return true;
14     }
15 
16     
17     void find(string s, int st, vector<string> &r, vector<vector<string> > &res){
18         if (st>=s.size()){
19             res.push_back(r);
20         }else{
21         for (int i=st;i<s.size();i++){            
22             if (valid(s,st,i)){
23                 r.push_back(s.substr(st,i-st+1));
24                 find(s,i+1,r,res);        
25                 r.pop_back();
26             }
27             
28         }
29         }  
30     }
31 
32     vector<vector<string>> partition(string s) {
33         // Start typing your C/C++ solution below
34         // DO NOT write int main() function
35         vector<vector<string> > res;
36         vector<string> r;
37         find(s,0,r,res);
38         return res;    
39     }
40 };

 

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

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:       }  
posted @ 2014-10-14 23:38  Ryan in C++  阅读(201)  评论(0编辑  收藏  举报