131. Palindrome Partitioning

18 篇文章 0 订阅
15 篇文章 0 订阅

回文分割。

DP+dfs。

先判断str中任意字串是否是回文,用动态规划方法:

//construct the pailndrome checking matrix
// 1) matrix[i][j] = true; if (i==j) -- only one char
// 2) matrix[i][j] = true; if (i==j+1) && s[i]==s[j] -- only two chars
// 3) matrix[i][j] = matrix[i+1][j-1]; if s[i]==s[j] -- more than two chars

然后再用dfs来不断的分割str。

有个问题:在dfs中,会出现重复判断某一子串是否是回文,以及计算该子串的所有回文分割的情况。这样应该浪费了很多时间。
可以保存之前出现子串的分割结果,后面遇到时直接访问下就可以了。

class Solution {
public:
    vector<vector<string>> partition(string s) {
        bool** mark = isPalindrome(s);
        vector<vector<string>> out;
        vector<string> temp;
        dfs(s, 0, s.size()-1, temp, out, mark);
        for(int i = 0; i < s.size()-1; ++i)
            delete [] mark[i];
        delete mark;
        return out;
    }
    bool** isPalindrome(string s){
        int size = s.size();
        bool** mark = new bool*[size];
        for(int i = 0; i < size; ++i)
            mark[i] = new bool[size];
        for(int i = size-1; i >= 0; --i){
            for(int j = i; j < size; ++j){
                if(i==j) mark[i][j] = true;
                if(j-i==1) mark[i][j] = (s[i]==s[j]);
                if(j-i>1) mark[i][j]=(s[i]==s[j])&&mark[i+1][j-1];
            }
        }
        return mark;
    }
    void dfs(string s, int i, int j, vector<string> temp, vector<vector<string>>& out, bool**& mark){
        if(i>j) out.push_back(temp);
        for(int k = i; k <= j; ++k){
            if(mark[i][k]) temp.push_back(s.substr(i,k-i+1));
            else continue;
            dfs(s,k+1,j,temp,out,mark);
            temp.pop_back();
         }
    }
};

//没有动态规划,直接判断子串是否回文的方法:

class Solution { //没有动态规划,直接判断子串是否回文的方法
public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> out;
        vector<string> temp;
        dfs(s, 0, s.size()-1, temp, out);
        return out;
    }
    void dfs(string s, int i, int j, vector<string> temp, vector<vector<string>>& out){
        if(i>j) out.push_back(temp);
        for(int k = i; k <= j; ++k){
            if(isPalindrome(s,i,k)) temp.push_back(s.substr(i,k-i+1));
            else continue;
            dfs(s,k+1,j,temp,out);
            temp.pop_back();
         }
    }
    bool isPalindrome(const string& s, int start, int end) {
        while(start <= end) {
            if(s[start++] != s[end--])
                return false;
        }
        return true;
    }
};


上面两种法子(加了DP和没加DP)运行时间都在40ms左右,加了dp效率并没有增加。而discuss中没有dp的却能跑到12ms。

我调试后发现问题在于:dfs中的变量path不是按引用传的。修改后:

    void dfs(string s, int i, int j, vector<string>& temp, vector<vector<string>>& out){
        if(i>j) out.push_back(temp);
        for(int k = i; k <= j; ++k){
            if(isPalindrome(s,i,k)) temp.push_back(s.substr(i,k-i+1));
            else continue;
            dfs(s,k+1,j,temp,out);
            temp.pop_back();
         }
    }


不论有没有加dp,时间都变成12ms了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值