leetcode 132 Palindrome Partitioning II
https://www.cnblogs.com/grandyang/p/4271456.html leetcode 132
https://www.cnblogs.com/grandyang/p/7404777.html leetcode 647
将字符串切割为回文的最小切割数:动态规划
p[i][j]表示s[i...j]是否为回文;dp[i]表示s[0...i]有多少种切割方法。
第一个循环i,遍历字符串中的每一个字符;第二个循环j,遍历0到i,在j处切割为[0,j-1]和[j,i],如果[j,i]为回文,则更新dp[i]。
class Solution { public: int minCut(string s) { if(s.empty()) return 0; int m=s.size(); vector<vector<bool>> p(m,vector<bool>(m,0)); vector<int> dp(m,0); for(int i=0;i<m;++i) { dp[i]=i; for(int j=0;j<=i;++j) { if(s[i]==s[j]&&(i-j<2||p[j+1][i-1])) { p[j][i]=true; dp[i]=j==0?0:min(dp[i],1+dp[j-1]); } } } return dp[m-1]; } };
相关推荐
aanndd 2020-08-12
aanndd 2020-07-26
aanndd 2020-07-08
zangdaiyang 2020-07-04
yaohustiAC 2020-06-28
us0 2020-06-28
yaohustiAC 2020-06-28
zangdaiyang 2020-06-28
Clairezz 2020-06-28
嗡汤圆 2020-06-26
嗡汤圆 2020-06-21
aanndd 2020-06-16
aanndd 2020-06-16
码墨 2020-06-16
yaohustiAC 2020-06-11
zangdaiyang 2020-06-10
jiayuqicz 2020-06-09
yaohustiAC 2020-06-06
heray0 2020-06-04