Dfs求出每一个回文就ok了,2刷自己刷代买练手
class Solution {
public:
bool isok(string s, int start, int end){
while(start <= end){
if(s[start++] != s[end--])
return false;
}
return true;
}
void dfs(int index, string s, vector<vector<string>>& ve, vector<string>& vec){
if(index == s.size()){
ve.push_back(vec);
return ;
}
for(int i = index; i < s.size(); ++ i){
if(isok(s, index, i)){
vec.push_back(s.substr(index, i - index + 1));
dfs(i + 1, s, ve, vec);
vec.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>>ve;
if(s.size() == 0) return ve;
vector<string>vec;
dfs(0, s, ve, vec);
return ve;
}
};