暴力= =
枚举断开位置...
据说可以dp,等下试试
class Solution { public: bool isPal(const string& s , int start , int end){ end --; while(start < end){ if(s[start] != s[end]) return false; start ++ ; end --; } return true; } void search(const string& s , vector<vector<string> > &ans , vector<string>& path , int prev , int end){ if(end >= s.size()){ if(isPal(s , prev , end)){ path.push_back(s.substr(prev , end-prev)); ans.push_back(path); path.pop_back(); } return; } //not break search(s , ans , path , prev , end + 1); //break if(isPal(s , prev , end)){ path.push_back(s.substr(prev,end-prev)); search(s , ans , path , end , end + 1); path.pop_back(); } } vector<vector<string>> partition(string s) { vector<vector<string> >ans; if(s.size() == 0) return ans; vector<string> path; search(s , ans , path , 0 , 1); return ans; } };
by 1957