首页 > 其他 > 详细

Palindrome Partitioning

时间:2014-04-25 07:41:58      阅读:478      评论: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"]
]

Solution: ...

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     vector<vector<string>> res;
 4     vector<vector<string>> partition(string s) {
 5         res.clear();
 6         vector<string> part;
 7         partitionRe(s, 0, part);
 8         return res;
 9     }
10 
11     void partitionRe(const string &s, int start, vector<string> &part) {
12         if (start == s.size())
13         {
14             res.push_back(part);
15             return;
16         }
17         string palindrom;
18         for (int i = start; i < s.size(); ++i) {
19             palindrom.push_back(s[i]);
20             if (!isPalindrome(palindrom)) continue;
21             part.push_back(palindrom);
22             partitionRe(s, i + 1, part);
23             part.pop_back();
24         }
25     }
26 
27     bool isPalindrome(const string &s) {
28         int i = 0, j = s.size()-1;
29         while (i < j) {
30             if (s[i] != s[j]) return false;
31             i++; j--;
32         }
33         return true;
34     }
35 };
bubuko.com,布布扣

 

Palindrome Partitioning,布布扣,bubuko.com

Palindrome Partitioning

原文:http://www.cnblogs.com/zhengjiankang/p/3682307.html

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