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

131. Palindrome Partitioning

时间:2016-09-05 15:27:52      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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

 

131. Palindrome Partitioning

标签:

原文地址:http://www.cnblogs.com/shadowwalker9/p/5842339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!