![在这里插入图片描述](https://img-blog.csdnimg.cn/20210711155113605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1c3VzdXFpbmd4aWFv,size_16,color_FFFFFF,t_70)
class Solution {
private:
vector<vector<string>> result;
vector<string> path;
bool isPalindrome(const string &str);
void backTracking(const string &s, int startIndex);
public:
vector<vector<string>> partition(string s)
{
backTracking(s, 0);
return result;
}
};
inline bool Solution::isPalindrome(const string &str)
{
int low = 0;
int high = str.size() - 1;
while (low < high)
{
if (str[low] != str[high])
return false;
low++;
high--;
}
return true;
}
inline void Solution::backTracking(const string &s, int startIndex)
{
if (startIndex == s.size())
{
result.push_back(path);
return;
}
for (int i = startIndex; i < s.size(); ++i)
{
string str = s.substr(startIndex, i - startIndex + 1);
if (isPalindrome(str))
{
path.push_back(str);
backTracking(s, i + 1);
path.pop_back();
}
}
}
void backtracking(参数)
{
if (终止条件)
{
存放结果;
return;
}
for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小))
{
处理节点;
backtracking(路径,选择列表);
回溯,撤销处理结果
}
}