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 { private: vector<vector<string>> res; vector<string> tempRes; public: bool isValid(string str) { stack<char> stk; for(int i=0;i<str.size();i++) stk.push(str[i]); for(int i=0;i<str.size();i++ ) { if(str[i]!=stk.top()) return false; else stk.pop(); } return true; } void getRes(string str) { if(str.size()==0) { res.push_back(tempRes); return ; } string temp; for(int i=0;i<str.size();i++) { temp+=str[i]; if(isValid(temp)) { tempRes.push_back(temp); getRes(str.substr(i+1)); tempRes.pop_back(); } } } vector<vector<string>> partition(string s) { getRes(s); return res; } };