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

leetcode 131. Palindrome Partitioning

时间:2015-01-31 11:54:13      阅读:202      评论: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 vector <vector<string> > results;
 2     vector <string> line;
 3     vector<vector<string>> partition(string s) 
 4     {
 5         getPartition(s, 0);
 6         return results;
 7     }
 8     
 9     void getPartition(string s, int index)
10     {
11         if (index >= s.size())
12         {
13             results.push_back(line);
14             return;
15         }
16         
17         for (int i = index; i < s.size(); i++)
18         {
19             string ts = s.substr(index, i + 1 - index);
20             if (isPalindrome(ts))
21             {
22                 line.push_back(ts);
23                 getPartition(s, i + 1);
24                 line.pop_back();
25             }
26         }
27     }
28     
29     bool inline isPalindrome(string s)
30     {
31         int i = 0, j = s.size() - 1;
32         while (i < j)
33         {
34             if (s[i] == s[j])
35                 i++, j--;
36             else
37                 return false;
38         }
39         
40         return true;
41     }

 

leetcode 131. Palindrome Partitioning

标签:

原文地址:http://www.cnblogs.com/ym65536/p/4263711.html

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