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

 

 1 class Solution {
 2 public:
 3     void dfs(string s,vector<string> &path,vector<vector<string> >&res)
 4     {
 5 
 6         if(s.size()<1) 
 7         {
 8             res.push_back(path);
 9             return ;
10         }
11 
12         for(int i=0;i<s.size();i++)
13         {
14             int begin=0;
15             int end=i;
16 
17             while(begin<end)
18             {
19                 if(s[begin]==s[end])
20                 {
21                     begin++;
22                     end--;
23                 }else
24                 {
25                     break;
26                 }
27             }
28 
29             if(begin>=end)
30             {
31                 path.push_back(s.substr(0,i+1));
32                 dfs(s.substr(i+1),path,res);
33                 path.pop_back();
34             }
35         
36         }
37     }
38     vector<vector<string>> partition(string s) {
39         vector<vector<string> >res;
40         vector<string> path;
41         dfs(s,path,res);
42         return res;
43     }
44 };

 

posted on 2015-06-06 21:21  黄瓜小肥皂  阅读(158)  评论(0编辑  收藏  举报