LeetCode------------palindrome-partitioning(回文)

来源:互联网 发布:刀剑乱舞极化数据 编辑:程序博客网 时间:2024/06/05 16:51

题目描述


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

class Solution {
public:
    
    void dfs(string s, vector<vector<string>> &res, vector<string> &path){
        if (s.size()==0){
            res.push_back(path);
            return ;                   //一条深度路径结束,递归出口
        }
        for (int i=0; i<s.size(); i++){
            int begin = 0;
            int end = i;
            while (begin < end){
                if (s[begin] == s[end]){
                    ++begin;
                    --end;
                }else 
                    break;
            }
            if (begin >= end){
                path.push_back(s.substr(0, i+1));
                dfs(s.substr(i+1,s.size()), res, path);
                path.pop_back();                  //逐步回退清空路径。。。。。
            }
        }
    }
    
    vector<vector<string>> partition(string s) {
        vector<vector<string>> res;
        vector<string> path;
        
        dfs(s, res, path);
        return res;
    }
};
0 0
原创粉丝点击