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"] ]
思路:总体想法同restoreIpAddresses相似,采用遍历整个字符串的方式。
//3333333333333333333333333333333 //判断字符串是否是回文 bool isPalindrome2(string s) { if(s.size()<=1) return true; int startIndex=0; int endIndex = s.size()-1; while(startIndex<=endIndex){ if(s[startIndex]==s[endIndex]){ startIndex++; endIndex--; continue; } return false; } return true; } vector<vector<string> > partition(string s) { vector<vector<string> > result; vector<string> partTmp; dfsPart(s,0,result,partTmp); return result; } void dfsPart(string s, int start, vector<vector<string> > &result,vector<string> partTmp){ //结束,合理的情况 if(start==s.size()){ result.push_back(partTmp); } string t; for(int i=start; i<s.size(); i++){ t += s[i]; if(isPalindrome2(t)){ partTmp.push_back(t); dfsPart(s,i+1,result,partTmp); partTmp.pop_back(); } } } //3333333333333333333333333333333333