回文最少分割数
作者:互联网
【题目】给定一个字符串str,返回把str全部切成回文子串的最小分割数。
public class MinCut {
public static int minCut(String str) {
if (str == null || str.equals("")) {
return 0;
}
char[] chas = str.toCharArray();
int len = chas.length;
int[] dp = new int[len + 1];// dp数组
boolean[][] b = new boolean[len][len];
dp[len - 1] = -1;
for (int i = len - 1; i > -1; i--) {
dp[i] = Integer.MAX_VALUE;
for (int j = i; j < len; j++) {
if (chas[i] == chas[j] && (j - i < 2 || b[i + 1][j - 1])) {
b[i][j] = true;
dp[i] = Math.min(dp[i], dp[j + 1] + 1);
}
}
}
return dp[0];
}
}
标签:分割,int,len,chas,最少,str,new,dp,回文 来源: https://blog.csdn.net/gkq_tt/article/details/88078944