首页 > 其他 > 详细

【leetcode】Palindrome Partitioning

时间:2015-06-06 21:55:56      阅读:267      评论:0      收藏:0      [点我收藏+]

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 };

 

【leetcode】Palindrome Partitioning

原文:http://www.cnblogs.com/jawiezhu/p/4557324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!