本文最后更新于614 天前,其中的信息可能已经过时,如有错误请发送邮件到tomding1065@gmail.com
https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html
视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6
1.这个题的是做字符串的分割,关键的思路在于分割的位置,收集子串的是否为回文串,对于下标的敏感度,而且本题还是不允许重复元素,这个题重要的思路,代码其实不难,以及对于标准库函数的熟练strsub。
CPP
class Solution {
public:
vector<vector<string>> res;
vector<string> path;
bool isPalindrome(const string& s, int begin, int end){
for(int i = begin, j = end; i < j; i++, j--){
if(s[i] != s[j]){
return false;
}
}
return true;
}
void backtracking(const string& s, int startIndex){
if(startIndex >= s.size()){
res.push_back(path);
return ;
}
for(int i = startIndex; i < s.size(); i++){
if(isPalindrome(s, startIndex, i)){
string str = s.substr(startIndex, i - startIndex + 1);
path.push_back(str);
}else{
continue;
}
backtracking(s, i + 1);
path.pop_back();
}
}
vector<vector<string>> partition(string s) {
path.clear();
res.clear();
backtracking(s, 0);
return res;
}
};