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

class Solution {
public:
    
    void dfs(string s, vector> &res, vector &path){
        if (s.size()==0){
            res.push_back(path);
            return ;                   //一条深度路径结束,递归出口
        }
        for (int i=0; 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> partition(string s) {
        vector> res;
        vector path;
        
        dfs(s, res, path);
        return res;
    }
};

你可能感兴趣的:(leetcode,字符串)