题目描述
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"] ]
public:
void dfs(string s, vector<vector<string>> &res, vector<string> &path){
if (s.size()==0){
res.push_back(path);
return ; //一条深度路径结束,递归出口
}
for (int i=0; i<s.size(); i++){
int begin = 0;
int end = i;
while (begin < end){
if (s[begin] == s[end]){
++begin;
--end;
}else
break;
}
if (begin >= end){
path.push_back(s.substr(0, i+1));
dfs(s.substr(i+1,s.size()), res, path);
path.pop_back(); //逐步回退清空路径。。。。。
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> path;
dfs(s, res, path);
return res;
}
};