Palindrome Partitioning II
来源:互联网 发布:冒险岛2韩服pk数据榜 编辑:程序博客网 时间:2024/06/01 08:28
题目原型:
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.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
基本思路:
题目的意思就是在上一题的基础上找出切分次数最少的组合,即尽可能让回文字符串更长。那么我们同样生成table表用来存储i到j之间的字符串是否是回文;然后遍历字符串来更新最小的切分次数。
public int minCut(String s){if(s==null||s.length()==0)return 0;//创建一个table表boolean[][] table = new boolean[s.length()][s.length()];int[] rs = new int[s.length()];for(int i = 0;i<table.length;i++){for(int j = 0;j<table.length;j++)table[i][j] = true;}//产生table表genTable(table,s);if(table[0][table.length-1])return 0;for(int i = 1;i<s.length();i++){if(table[0][i]){rs[i] = 0;continue;}rs[i] = rs[i-1]+1;//不要s[i]参与//要s[i]参与for(int j = 0;j<i;j++){if(table[j][i])rs[i] = Math.min(rs[i],rs[j-1]+1);}}return rs[rs.length-1];}//产生table表public void genTable(boolean[][] table,String s){//根据间距从小到大来遍历for(int dis = 1;dis<table.length;dis++){for(int i = 0,j = i+dis;j<table.length;i++,j++){table[i][j] = (table[i+1][j-1]&&(s.charAt(i)==s.charAt(j)));}}}
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
- POJ-1988 Cube Stacking 带权并查集
- 玩一玩javascript的bookmarklet
- asp.net动态引用WebService
- 线程sleep()理解
- 导入工程时出现Unable to resolve target 'android-18' error解决办法
- Palindrome Partitioning II
- C语言基础四_预处理和变量类型
- 电脑和手机之间使用蓝牙传输文件-步骤
- 阿斯顿撒旦撒大大说的
- Apache吃空内存,频繁宕机
- MAC 系统清理
- CocosBuilder 多分辨率基础 分享2
- Foundation框架NSDictionary
- 大数据的存储和管理