1 Star 0 Fork 0

Roderland/algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Question132.java 959 Bytes
一键复制 编辑 原始数据 按行查看 历史
Roderland 提交于 2021-03-10 15:24 +08:00 . ====== roderland ======
package question;
/**
* @author Roderland
* @since 1.0
*/
public class Question132 {
public static void main(String[] args) {
System.out.println(new Question132().minCut("aab"));
}
public int minCut(String s) {
int n = s.length();
int[][] dp = new int[n][n];
for (int k = 1; k < n; k++) {
for (int i = 0; i + k < n; i++) {
int j = i + k;
if (!is(s.substring(i, j + 1))) {
dp[i][j] = Integer.MAX_VALUE;
for (int l = i; l < j; l++) {
dp[i][j] = Math.min(dp[i][j], dp[i][l] + dp[l + 1][j] + 1);
}
}
}
}
return dp[0][n - 1];
}
public boolean is(String s) {
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) return false;
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Roderland/algorithm.git
git@gitee.com:Roderland/algorithm.git
Roderland
algorithm
algorithm
master

搜索帮助