码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Palindrome Partitioning II

时间:2015-03-21 18:25:43      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

Notes:

1. If len < 2, return 0. It is not minimum length but how many cuts.

2. As it was cutting inside of string. Final result is dp[0] - 1, since there is no cut at most left.

 1 class Solution {
 2 public:
 3     int minCut(string s) {
 4         int len = s.size();
 5         if (len < 2) return 0;
 6         vector<int> dp(len+1, 0);
 7         vector<vector<bool> > rec(len, vector<bool>(len, false));
 8         for (int i = 0; i <= len; i++) dp[i] = len - i;
 9         for (int i = len-1; i >= 0; i--) {
10             for (int j = i; j < len; j++) {
11                 if (s[i] == s[j] && (j - i < 2 || rec[i+1][j-1])) {
12                     rec[i][j] = true;
13                     dp[i] = min(dp[i], dp[j+1]+1);
14                 }
15             }
16         }
17         return dp[0]-1;
18     }
19 };

 

LeetCode – Refresh – Palindrome Partitioning II

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!