码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Palindrome Partitioning

时间:2015-06-06 21:57:41      阅读:122      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!