字符串中的回文串组合
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"] ]
这个有点像二叉树路径,需要递归
class Solution {
public:
void findpath(string s,vector<string>&path,vector<vector<string>>&res)
{
if(s.size()<1)
res.push_back(path);//到字符串尾部
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));
findpath(s.substr(i+1),path,res);
path.pop_back();//弹出查找的字符串
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>>res;
vector<string>path;
findpath(s,path,res);
return res;
}
};