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"]
]
难度系数:
中等
实现
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;
}
时间: 2024-10-10 10:29:02