Palindrome Partitioning II
来源:互联网 发布:单元格数据拆分 编辑:程序博客网 时间:2024/05/19 05:31
<span style="font-family: Arial, Helvetica, sans-serif;">public class Solution {</span>
public int minCut(String s) { if (s.length() == 1){ return 0; }boolean[][] matrix = matrixMark(s);int [] cut = new int[s.length()+1];for(int i = 0; i < cut.length; i++){cut[i] = i;} cut[0] = 0; for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if (matrix[j][i - 1] ) { cut[i] = Math.min(cut[i], cut[j] + 1); // matrix[j]表示的是从位置之后的分割到最后i的位置是不是有一个串. // cut[i]表示在这个位置之前的最小分割数 // 比如 aacbca cut[1]表示位置1的最小分割,而matrix[1][3]表示的是a之后的位置acb是不是回文串 } } } return cut[s.length()] - 1;}boolean [][] matrixMark(String s){boolean [][] matrix = new boolean[s.length()][s.length()];for (int i = 0; i < s.length(); i++){matrix[i][i] = true;}for(int delta = 1; delta < s.length(); delta++){for (int i = 0; i + delta < s.length(); i++){int m = i;int n = i + delta;if ((n - m < 2 && s.charAt(m) == s.charAt(n)) || (s.charAt(m) == s.charAt(n) && matrix[m+1][n-1] )){matrix[m][n] = true;}}}return matrix;}}
0 0
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Palindrome Partitioning II
- Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Palindrome Partitioning II
- LetCode:Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- ajax终止请求
- 关于安卓知识的一些小记录
- python中字符大小写的控制及判断
- 在整个库里查某个值并替换
- 在 Xcode 6 中使用矢量图( iPhone 6 置配 UI)
- Palindrome Partitioning II
- 临界资源互斥的基本方法
- 求余运算转换为位运算
- mdd的烦恼
- activity的生命周期描述
- iOS8开发~Swift(三)UI详解
- Note6:继承和接口
- SQL的count函数实例
- 配置catalog并备份数据以及注册target中已有的备份片