标签:leetcode
Return all possible palindrome partitioning of s.
For example, given s = "aab", Return
[
["aa","b"],
["a","a","b"]
]
中等
bool isPalindrome(string &s, int ii, int jj)
{
for (int i = ii, j = jj; i < j; i++,j--) {
if (s[i] != s[j])
return false;
}
return true;
}
vector<vector<string> > partition(string s) {
vector<vector<string> > vvs;
int i = 0;
int j = 0;
if (s.size() == 1) {
vector<string> vs;
vs.push_back(s);
vvs.push_back(vs);
return vvs;
}
while (j < s.size()) {
if (isPalindrome(s, i, j)) {
if (j == s.size() -1) {
vector<string> vs;
vs.push_back(s);
vvs.push_back(vs);
} else {
vector<vector<string> > vvstr = partition(s.substr(j+1));
for (int k = 0; k < vvstr.size(); ++k) {
vvstr[k].insert(vvstr[k].begin(), s.substr(i, j-i+1));
vvs.push_back(vvstr[k]);
}
}
}
j++;
}
return vvs;
}
LeetCode131——Palindrome Partitioning
标签:leetcode
原文地址:http://blog.csdn.net/booirror/article/details/43986379