题目描述
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;
}
};

本文介绍了一种通过深度优先搜索实现的字符串回文划分算法,该算法能够找出所有可能的回文子串组合。文章提供了详细的代码实现过程,包括如何递归地检查字符串是否为回文,并将符合条件的子串加入到最终的解决方案中。
160

被折叠的 条评论
为什么被折叠?



