Leetcode: Palindrome Partitioning

来源:互联网 发布:在哪里编程游戏 编辑:程序博客网 时间:2024/05/29 14:33

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

随手写了一个,没考虑时间复杂度,觉得应该过不了,结果竟然过了。简单的DFS。

class Solution {public:    vector<vector<string>> partition(string s) {        vector<vector<string>> result;        if (s.empty()) return result;                vector<string> partition;        partitionUtil(result, partition, s, 0, s.size() - 1);    }        void partitionUtil(vector<vector<string>> &result, vector<string> &partition, string &s, int start, int end) {        if (start > end) {            result.push_back(partition);        }        else {            for (int i = start; i <= end; ++i) {                string str1 = s.substr(start, i - start + 1);                if (isPalindrome(str1)) {                    partition.push_back(str1);                    partitionUtil(result, partition, s, i+1, end);                    partition.pop_back();                }            }        }    }        bool isPalindrome(const string &str) {        for (int i = 0, j = str.size() - 1; i < j;) {            if (str[i++] != str[j--]) {                return false;            }        }                return true;    }};

=================第二次===============

相比之下,这种方法速度慢一些,感觉是因为vector的创建复制操作太多。

class Solution {public:    vector<vector<string>> partition(string s) {        vector<vector<bool>> palindromes(s.size(), vector<bool>(s.size(), false));        for (int i = s.size() - 1; i >= 0; --i) {            for (int j = i; j < s.size(); ++j) {                if (s[i] == s[j] && (j - i < 2 || palindromes[i+1][j-1])) {                    palindromes[i][j] = true;                }            }        }                return partitionUtil(palindromes, s, 0, s.size() - 1);    }        vector<vector<string>> partitionUtil(vector<vector<bool>> &palindromes,const string &s, int start, int end) {        vector<vector<string>> partitions;        for (int i = start; i <= end; ++i) {            if (palindromes[start][i]) {                string left = s.substr(start, i - start + 1);                vector<vector<string>> rights = partitionUtil(palindromes, s, i+1, end);                if (i >= end) {                    rights.push_back(vector<string>());                }                 for (int j = 0; j < rights.size(); ++j) {                    vector<string> comb;                    comb.push_back(left);                    comb.insert(comb.end(), rights[j].begin(), rights[j].end());                    partitions.push_back(comb);                }            }        }                return partitions;    }};
第一种方法:

class Solution {public:    vector<vector<string>> partition(string s) {        vector<vector<string>> partitions;        if (s.empty()) {            return partitions;        }                vector<vector<bool>> palindromes(s.size(), vector<bool>(s.size(), false));        for (int i = s.size() - 1; i >= 0; --i) {            for (int j = i; j < s.size(); ++j) {                if (s[i] == s[j] && (j - i < 2 || palindromes[i+1][j-1])) {                    palindromes[i][j] = true;                }            }        }                vector<string> comb;        partitionUtil(partitions, comb, palindromes, s, 0, s.size() - 1);        return partitions;    }        void partitionUtil(vector<vector<string>> &partitions, vector<string> &comb,                                         vector<vector<bool>> &palindromes,const string &s, int start, int end) {        if (start > end) {            partitions.push_back(comb);            return;        }                for (int i = start; i <= end; ++i) {            if (palindromes[start][i]) {                comb.push_back(s.substr(start, i - start + 1));                partitionUtil(partitions, comb, palindromes, s, i+1, end);                comb.pop_back();            }        }    }};


0 0
原创粉丝点击