PalindromePartitioning II
来源:互联网 发布:仙灵觉醒仙羽进阶数据 编辑:程序博客网 时间:2024/06/05 07:35
<pre name="code" class="java"> cut[i]记录i到end的最小切割数 matrix[i][j]记录从i到j的子串是否是回文串
public class PalindromePartitioning2 {public int minCut(String s) {if (s == null)return 0;int length = s.length();int[] cut = new int[length];Boolean[][] matrix = new Boolean[length][length];for (int i = 0; i < length; i++) {cut[i] = length - i - 1;}matrix[length - 1][length - 1] = true;for (int i = length - 1; i >= 0; i--) {for (int j = i; j < length; j++) {if (i == j)matrix[i][j] = true;else if (j - i == 1) {if (s.charAt(i) == s.charAt(j)) {matrix[i][j] = true;} elsematrix[i][j] = false;} else {if (matrix[i + 1][j - 1] && (s.charAt(i) == s.charAt(j))) {matrix[i][j] = true;} elsematrix[i][j] = false;}if (j < length - 1 && matrix[i][j] && cut[i] > (cut[j + 1] + 1)) {cut[i] = cut[j + 1] + 1;} else if (j == length - 1 && matrix[i][j])cut[i] = 0;}}return cut[0];}public static void main(String[] args) {PalindromePartitioning2 test = new PalindromePartitioning2();int min = test.minCut("aabc");System.out.println(min);}}
0 0
- PalindromePartitioning II
- leetcode17.PalindromePartitioning
- LeetCode PalindromePartitioning
- 每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
- ii
- ii
- ii
- II
- ii
- Permutations II 排列II
- Probabilistic Robotics ( II.II )
- ASC II
- Part II
- Translations II
- 横线II
- 分手II
- 童话II
- MRP II
- Eclipse 字体设置
- 私人使用地方
- jump game
- C语言拾遗:公有、私有和受保护的继承--《C++ primer》笔记
- ios和mac开发 学习资料
- PalindromePartitioning II
- 本地Yum软件源安装Cloudera Manager 5
- java 抽象类与接口的使用
- 为SUSE Linux添加软件安装源的方法
- metalink mos介绍
- JS随录-2
- 豌豆荚研发管理经验分享-软件项目管理及绩效考核方法
- 关于java中的读写文件
- 解决vs中编译c++项目时报类似:LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”错误