leetcode 132: Palindrome Partitioning II

来源:互联网 发布:暗影格斗2mac破解 编辑:程序博客网 时间:2024/06/06 03:06

Use two dp array, dp[i][j] means whether s[j...i] is a palindrome and cut[i] means the min cut before index i.

class Solution {public:    int minCut(string s) {        int len=s.length();        vector<bool> a(len,0);        vector<vector<bool> > dp(len,a);        vector<int> cut(len+1);        for(int i=0;i<=len;i++)            cut[i]=i-1;        for(int i=0;i<len;i++)            for(int j=i;j>=0;j--)            {                if(i-j<2)                    dp[i][j]=s[i]==s[j];                else                    dp[i][j]=s[i]==s[j]&&dp[i-1][j+1];                if(dp[i][j])                    cut[i+1]=min(cut[i+1],cut[j]+1);            }        return cut[len];    }};


0 0
原创粉丝点击