leetcode Palindrome I and II comparison

来源:互联网 发布:wifi局域网劫持软件 编辑:程序博客网 时间:2024/05/21 10:41

The easier one "Palindrome I" is solved by DFS ALG

1. From left to right, judge the substr(0, i) is Palindrome or not. If it is, add it to one of the element of the answer. Then use partition to find the remaining palindrome result for substr(i+1).


This version much like brute force. 

class Solution {public:    int isPalindrome(string s) {        int len = s.length();        int i;        for (i = 0; i < len/2; i++) {            if (s[i] != s[len-1-i])                return 0;        }        return 1;    }    vector<vector<string>> partition(string s) {        vector<vector<string>> ret;        vector<string> tmp;        vector<vector<string>> sub_ret;        int i, j, k;        for (i = 0; i < s.length(); i++) {            if (isPalindrome(s.substr(0,i+1))) {                tmp.push_back(s.substr(0,i+1));                sub_ret = partition(s.substr(i+1,s.length()-i-1));                if (sub_ret.size() == 0) {                    ret.push_back(tmp);                }                else {                    for (j = 0; j < sub_ret.size(); j++) {                    //TODO initialize here everytime is better than erase everytime ?                        for (k = 0; k < sub_ret[j].size(); k++) {                            tmp.push_back(sub_ret[j][k]);                        }                        ret.push_back(tmp);                        tmp.erase(tmp.begin() + 1,tmp.end());                    }                }                tmp.erase(tmp.begin(),tmp.begin()+1);            }        }        return ret;    }};

This version is actually DFS.


class Solution {public:    int isPalindrome(string s, int st, int end) {        while (st < end) {            if (s[st] != s[end])                return 0;            else {                st ++;                 end --;             }           }           return 1;    }        void subPartition(string s, vector<string> &tmp, vector<vector<string> > &res) {        if (s == "") {            res.push_back(tmp);            return;        }           int len, i;        len = s.length();        for (i = 0; i < len; i++) {            if (isPalindrome(s, 0, i)) {                tmp.push_back(s.substr(0,i+1));                subPartition(s.substr(i+1), tmp, res);                tmp.pop_back();            }           }       }       vector<vector<string> > partition(string s) {        vector<vector<string> > res;        vector<string> tmp;        if (s == "")             return res;        subPartition(s, tmp, res);        return res;    }   };

Each element in the tmp should be poped out after its later part of string is calculated well. The push operation and the pop operation should be matched. Otherwise erase operation should be used (like version 1 solution).

DFS must use pop operation. Make sure define some ending operation, like (s == "") in this problem.  Although version 1 is accepted by leetcode, but it use more time than version 2. Those "delete"  and "insert" operations are much time-consuming.


For harder problem "Palindrome ii" is solved by Dynamic Programming ALG. (DFS will exceed time limit)

class Solution {public:    int minCut(string s) {        int len, i, j;        len = s.length();        int* min_res;        int** is_pal; // leetcode compiler do not support "int a[len]"        min_res = new int[len+1];        is_pal = new int*[len];        for (i = 0; i < len; i++) {            min_res[i] = len - i - 1;            is_pal[i] = new int[len];            for (j = 0; j < len; j++) {                is_pal[i][j] = 0;            }        }        min_res[len] = -1;        for (i = len - 1; i >= 0; i--) {            for (j = i; j < len; j++) {                if (s[i] == s[j] && (j - i < 2 || is_pal[i+1][j-1])) {                    is_pal[i][j] = 1;                    min_res[i] = min(min_res[i], 1 + min_res[j + 1]);                }            }        }        return min_res[0];    }};


0 0
原创粉丝点击