132. Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Example:

Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
class Solution {
    public int minCut(String s) {
        int l = s.length();
        int[][] dp = new int[l][l];
        int[] c = new int[l + 1];
        for(int i = l - 1; i >= 0; i--){
            c[i] = Integer.MAX_VALUE;
            for(int j = i; j < l; j++){
                if(s.charAt(i) == s.charAt(j) && (j-i <= 1 || dp[i+1][j-1] == 1)){
                    dp[i][j] = 1;
                    c[i] = Math.min(c[i], c[j+1] + 1);
                }
            }
        }
        return c[0] - 1;
    }
}

https://blog.csdn.net/jin_kwok/article/details/51423222

碉堡的方法。

设两个数组,一个二维数组记录index为i,j的字符串是否为回文,另一个记录i,最后一个字符为回文的最小cut数。

 

 

 应该是cut【i】= min(1 + cut【j+1】),前提条件DP【i】【j】= 1.

 

posted @ 2019-10-05 11:15  Schwifty  阅读(148)  评论(0编辑  收藏  举报