【字符串】分割回文串

题目:

解答:

 1 class Solution {
 2 public:
 3     
 4     vector<vector<string>> partition(string s) 
 5     {
 6         vector<string> temp;
 7         vector<vector<string>> result;
 8         
 9         getPartition( s, temp, result);
10         
11         return result;
12     }
13     
14     void getPartition(string s, vector<string> temp, vector<vector<string>>& result) 
15     {
16         if(s.size() == 0)
17         {
18             result.push_back(temp);
19         }
20         else 
21         {
22             for(int i = 1; i <= s.size(); i++) 
23             {
24                 string subs = s.substr(0, i);
25                 if(isPalindrome(subs)) 
26                 {  
27                     temp.push_back(subs);
28                     getPartition(s.substr(i, s.size()), temp, result);
29                     temp.pop_back();
30                 }
31             }
32         }
33     }
34     
35     bool isPalindrome(string s) 
36     {
37         // 转换为小写字母
38         transform(s.begin(),s.end(),s.begin(),::tolower);
39         
40         int i = 0;
41         int j = s.size() - 1;
42         
43         while(i < j) 
44         {
45             if(s[i++] != s[j--])
46             {
47                 return false;
48             }
49         }
50         return true;
51     }
52 };

猜你喜欢

转载自www.cnblogs.com/ocpc/p/12899991.html
今日推荐