LeetCode-Palindrome Partitioning

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"]
  ]

输出总共2^n种,硬做就可以了
class Solution {
public:
    void sub(vector<vector<string> >&ret,vector<vector<bool> >&ISP,vector<string>& one,int start,string& s){
        if(start==s.length()){
            ret.push_back(one);
            return;
        }
        int size=one.size();
        int i=1;
        while(i+start<=s.length()){
            if(ISP[start][i]){
                one.push_back(s.substr(start,i));
                sub(ret,ISP,one,start+i,s);
                one.resize(size);
            }
            i++;

        }
    }
    bool isPalindrome(string&s,int start,int len){
        for(int i=0;i<len/2;i++){
            if(s[start+i]!=s[start+len-i-1])return false;
        }
        return true;
    }
    vector<vector<string> > partition(string s) {
        vector<vector<string> > ret;
        if(s=="")return ret;
        vector<vector<bool> > ISP;
        ISP.resize(s.length());
        for(int i=0;i<s.length();i++){
            ISP[i].resize(s.length()-i+1);
            for(int j=1;j<ISP[i].size();j++){
                if(isPalindrome(s,i,j))ISP[i][j]=true;
            }
        }
        vector<string> one;
        sub(ret,ISP,one,0,s);
        return ret;
    }
};
View Code
原文地址:https://www.cnblogs.com/superzrx/p/3356312.html