Palindrome Partitioning(C++分割回文串)
(1)回溯
class Solution {
private:
vector<vector<string>> vec;
public:
bool judge(string &s,int start,int end) {
while(start<end) {
if(s[start]!=s[end]) return false;
start++;
end--;
}
return true;
}
void helper(vector<string> v,string &s,int start) {
if(start>=s.length()) vec.push_back(v);
for(int i=start;i<s.length();i++) {
if(judge(s,start,i)) {
v.push_back(s.substr(start,i-start+1));
helper(v,s,i+1);
v.pop_back();
}
}
return;
}
vector<vector<string>> partition(string s) {
vector<string> v;
helper(v,s,0);
return vec;
}
};
还没有评论,来说两句吧...