算法描述:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
解题思路:所有可能的回文,可以用回溯法。
vector<vector<string>> partition(string s) { vector<vector<string>> results; vector<string> temp; backtracking(results, temp, s, 0); return results; } void backtracking(vector<vector<string>>& results, vector<string>& temp, string s, int index){ if(index == s.size()){ results.push_back(temp); return; } for(int i=index; i< s. size(); i++){ string ts = s.substr(index,i-index+1); if(isPalindrome(ts)){ temp.push_back(ts); backtracking(results,temp,s,i+1); temp.pop_back(); } } } bool isPalindrome(string ts){ int l =0; int r = ts.size()-1; while(l <= r){ if(ts[l++]!=ts[r--]) return false; } return true; }
LeetCode-131-Longest Palindromic Characters
原文:https://www.cnblogs.com/nobodywang/p/10353196.html