LeetCode Palindrome Partitioning II
来源:互联网 发布:netstat安装 centos 编辑:程序博客网 时间:2024/06/05 21:57
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.
一般DP,超时了,因为没有将判断回文字的部分优化。
import java.awt.Image;public class Solution {boolean isPalindrome(String s, int u,int v){int head = u;int tail = v;while(head<tail){if(s.charAt(head)!=s.charAt(tail)){return false;}head++;tail--;}return true;} public int minCut(String s) { if(s==null)return 0; if(isPalindrome(s, 0, s.length()-1)){ System.out.println(0); return 0; } int n = s.length();boolean[][] PalindromePair = new boolean [n][n];for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){PalindromePair[i][j]=isPalindrome(s, i, j); }}int [] []DpCount = new int [n][n];for(int i=0;i<n;i++){DpCount[i][i]=0;for(int j=i+1;j<n;j++){if(PalindromePair[i][j]){DpCount[i][j]=0;}else{DpCount[i][j]=j-i;}}}for(int k=1;k<n;k++){for(int i=0;i<n-k;i++){for(int j=i+1;j<k+i;j++){DpCount[i][i+k]=Math.min(DpCount[i][i+k], DpCount[i][j]+DpCount[j+1][i+k]+1);}}}return DpCount[0][n-1]; } public static void main(String[]args){ Solution solution = new Solution(); String string = new String("aab"); System.out.println(solution.minCut(string)); }}
下面的算法实际上是两个动态规划算法的结合,一个是 动态规划求回文字,一个是动态规划求前i个字符有多少个回文字。
DPCount[i]表示子串[0,i]最少切割多少次
public class Solution { public int minCut(String s) { if(s==null)return 0; int n = s.length(); if(n<2)return 0; boolean[][] PalindromePair = new boolean [n][n];int [] DPCount = new int [n];for(int i=0;i<n;i++){DPCount[i]=i;}for(int i=0;i<n;i++){if(s.charAt(0)==s.charAt(i)&&((i<2)||PalindromePair[1][i-1])){PalindromePair[0][i]=true;DPCount[i]=0;}for(int j=1;j<=i;j++){if(s.charAt(j)==s.charAt(i)&&((i-j<2)||PalindromePair[j+1][i-1])){PalindromePair[j][i]=true;DPCount[i]=Math.min(DPCount[i], DPCount[j-1]+1);}}}return DPCount[n-1]; } public static void main(String[]args){ Solution solution = new Solution(); String string = new String("aab"); System.out.println(solution.minCut(string)); }}
0 0
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- CSS样式(背景、文本、字体)学习笔记
- UVA 12558 Egyptian Fractions (HARD version) IDA*
- js跨域提交表单【详细教程,包解决】
- codeforces 489A
- ubantu安装vmare tool
- LeetCode Palindrome Partitioning II
- 多表连查的 left join 使用
- qt获取本机网络信息
- 第十四周项目6-阅读程序2(对a[i++]=的理解)
- 2014ACM/ICPC亚洲区北京站-重现赛
- php ajax 实例
- IOS开发---C语言-㉑预处理指令:宏定义、条件编译、文件包含
- 杭电2041
- python的re