LeetCode Palindrome Partitioning

来源:互联网 发布:php函数手册 编辑:程序博客网 时间:2024/06/01 08:56

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:    bool isPalindrome(const string &s, int first, int last) {        if (first == last)            return true;        while (first < last) {            if (s[first]!=s[last])                return false;            first++;            last--;        }        return true;    }        void _partition(const string &s, int first, vector<string> par) {        if (first == s.length())                result.push_back(par);        int i;        for (i=first;i<s.length();i++) {            if (isPalindrome(s, first, i)) {                par.push_back(s.substr(first, i-first+1));                _partition(s, i+1, par);                par.pop_back();            }        }    }        vector<vector<string> > partition(string s) {        result.clear();        vector<string> par;        _partition(s, 0, par);        return result;    }    vector<vector<string> > result;};


0 0
原创粉丝点击