leetcode131分割回文串

 

 

class Solution {
public:
    vector<vector<string>> ans;

    bool isok(string s){
        int i=0;
        int j=s.size()-1;
        while(i<j){
            if(s[i]!=s[j]){
                return false;
            }
            i++;j--;
        }
        return true;
    }

    void backtrack(string s, vector<string> cur,int lastindex){
        if(lastindex>=s.size()){
            ans.push_back(cur);
            return;
        }
        for(int i=lastindex;i<s.size();i++){
            string stemp=s.substr(lastindex,i-lastindex+1);
            if(isok(stemp)){
                cur.push_back(stemp);
                backtrack(s,cur,i+1);
                cur.pop_back();
            }
        }
    }

    vector<vector<string>> partition(string s) {
        vector<string> cur;
        backtrack(s,cur,0);
        return ans;
    }
};

 

posted @ 2019-11-29 11:57  Joel_Wang  阅读(150)  评论(0编辑  收藏  举报