首页 > 其他 > 详细

Palindrome Partitioning II

时间:2014-02-06 17:18:38      阅读:355      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣
 1 public class Solution {
 2     public int minCut(String s) {
 3         int len = s.length();
 4         int D[] = new int [len+1];
 5         for(int i=0;i<=len;i++){
 6             D[i] = len-i-1; //this means for example: abc cut 2
 7         }
 8         boolean [][]p = new boolean[len][len];
 9         for(int i=len-1;i>=0;i--){
10             for(int j=i;j<len;j++){
11                 if(s.charAt(i)==s.charAt(j)&&(j-i<2 ||p[i+1][j-1])){
12                     D[i] = Math.min(D[i],D[j+1]+1);
13                     p[i][j] = true;
14                 }
15             }
16         }
17         return D[0];
18     }
19 }
View Code

Palindrome Partitioning II

原文:http://www.cnblogs.com/krunning/p/3538799.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!