[LeetCode] Palindrome Partitioning

来源:互联网 发布:淘宝鱼塘是什么意思 编辑:程序博客网 时间:2024/04/30 22:26

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:    vector<vector<string> >res;    vector<string>v;    vector<vector<string>> partition(string s) {        dfs(s);        return res;    }    void dfs(string s){        if(s.size() < 1){            res.push_back(v);            return;        }        for(int i = 0;i < s.size();i ++){            int beg = 0,end = i;            while(beg < end){                if(s[beg] == s[end]){                    beg ++;                    end --;                }                else                    break;            }            if(beg >= end){                v.push_back(s.substr(0,i + 1));                dfs(s.substr(i+1));                v.pop_back();            }        }    }};


0 0
原创粉丝点击