题目:
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"]
]
分析:
回文。dfs。
代码:
class Solution {
public:
vector<vector<string>> res;
string now="";
vector<vector<string>> partition(string s) {
if(s.length()<1)return res;
now=s;
vector<string> rec;
dfs(rec,0,0);
return res;
}
void dfs(vector<string> rec,int start,int total){
if(start==now.length()&&total==now.length()){
res.push_back(rec);
return;
}
string t="";
for(int i=start;i<now.length();++i){
t=t+now[i];
if(isP(t)){
rec.push_back(t);
dfs(rec,i+1,total+i-start+1);
rec.erase(rec.end()-1);
}
}
}
bool isP(string s){
int i=0,j=s.length()-1;
while(i<j){
if(s[i]!=s[j])return false;
i++,j--;
}
return true;
}
};

3344

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



