132. Palindrome Partitioning II
Posted wentiliangkaihua
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
以上是关于132. Palindrome Partitioning II的主要内容,如果未能解决你的问题,请参考以下文章
131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
132. Palindrome Partitioning II
leetcode 132 Palindrome Partitioning II
lintcode-medium-Palindrome Partition II