leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)

题目:

Palindrome Partitioning

   I

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]

分析:

题意给我们一个字符串,求出所有这个字符串的子串,并且子串都为回文字符串的情况,输出它的集合结果


解题思路:(跟DFS深度遍历有点像!)

字符串Str = "aab";

分析了题目的数据之后,我们知道它的结果,可能是 a, a, b 或者  aa, b 这样的情况!

也就是说,我们需要去考虑 i = 1,  2 ....  n 的情况,比如

Str = "aaa"

结果集

[[a, a, a], [a, aa], [aa, a], [aaa]]

根据这样的情况,我们用类似于DFS的算法

str1 = str.substr(0, i); 取出前面下标从 0 开始到 i 结束的子串,判断str1是否满足回文字符串的要求,

1. 满足:这样子,有可能成为一种分割的情况,所以我们new出一个list集合,把str1放入到list中,然后我们求出str剩余的子串  str2 = str.substr(i); 取出前面下标从 i 开始到整个字符串结尾的子串, 然后将str2 作为新的字符串,同时把list集合也传入到函数中,递归调用。递归的结束条件就是到传入的这个字符串的长度为0(这样就意味着已经到了字符串的末尾了),此时证明这种情况下得到的list集合是满足条件的,我们把这个list集合 加入到 结果集合result中。

2. 不满足的话,继续 i++, 直到 i == str.length();

3. 全部结束之后,返回 最终的结果集合 result



AC代码:

[java]  view plain copy print ?
  1. package copylist;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5.   
  6. public class Solution {  
  7.     /* 
  8.      * 供外部调用 
  9.      * */  
  10.     public ArrayList<ArrayList<String>> partition(String s) {  
  11.         ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();  
  12.         ArrayList<String> list = new ArrayList<String>();  
  13.           
  14.         if (s == null || s.length() == 0)  
  15.             return result;  
  16.           
  17.         calResult(result,list,s);  
  18.         return result;  
  19.     }  
  20.     /** 
  21.      * 判断一个字符串是否是回文字符串 
  22.      * i -> str[0] 
  23.      * j -> str[len-1] 
  24.      * i:往后移 
  25.      * j:往前移 
  26.      * @param str 
  27.      * @return  
  28.      */  
  29.     private boolean isPalindrome(String str){  
  30.           
  31.         int i = 0;  
  32.         int j = str.length() - 1;  
  33.         while (i < j){  
  34.             if (str.charAt(i) != str.charAt(j)){  
  35.                 return false;  
  36.             }  
  37.             i++;  
  38.             j--;  
  39.         }  
  40.         return true;  
  41.     }  
  42.       
  43.     /** 
  44.      *  
  45.      * @param result : 最终要的结果集 ArrayList<ArrayList<String>> 
  46.      * @param list : 当前已经加入的集合 ArrayList<String> 
  47.      * @param str : 当前要处理的字符串 
  48.      */  
  49.     private void calResult(ArrayList<ArrayList<String>> result  
  50.             , ArrayList<String> list  
  51.             , String str)  
  52.     {  
  53.         //当处理到传入的字符串长度等于0,则这个集合list满足条件,加入到结果集中  
  54.         if (str.length() == 0)  
  55.             result.add(new ArrayList<String>(list));  
  56.         int len = str.length();  
  57.         //递归调用  
  58.         //字符串由前往后,先判断str.substring(0, i)是否是回文字符串  
  59.         //如果是的话,继续调用函数calResult,把str.substring(i)字符串传入做处理  
  60.         for (int i=1; i<=len; ++i){  
  61.             String subStr = str.substring(0, i);  
  62.             if (isPalindrome(subStr)){  
  63.                 list.add(subStr);  
  64.                 String restSubStr = str.substring(i);  
  65.                 calResult(result,list,restSubStr);  
  66.                 list.remove(list.size()-1);  
  67.             }  
  68.         }  
  69.     }  
  70.       
  71.     public static void main(String[] args) {  
  72.         System.out.println(new Solution().partition("aabaa"));  
  73.     }  
  74. }  


题目:(DP)

Palindrome Partitioning II

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做的,但上一题我们没有用那种方法,但是这题要用DP哈,不然会TLE哦)


解题思路:

我们可以把这个问题转换成动态规划dp的问题


首先我们先定义几个变量,并对这几个量做一定的说明!为了方便理解,下面这些为伪码!!!

len  = str.length();   // 字符串的长度

int[] cuts = new int[len + 1]; //cuts数组,cuts[i] 表示 以 i 开头到len结尾的子串 要达到题意需要的最少切割数(这样子最终 cuts[0]就是我们要的结果)【初始化 cuts[i] = len - i, 因为最坏情况以 i 开头到len结尾的子串要切割数就是每个字符都切一次】

int[][] matrix = new  int[len][len]; //设置出一个邻接矩阵数组,它所表示的意思:如matrix[i][j] = true, 表示 子串 sub(i, j) 是满足回文字符串条件的!

那么判断matrix[i][j] 是否满足回文字符串的条件是: 


matrix[i+1][j-1] == true (表示sub(i+1,j-1)是满足回文字符串) && str[i] == str[j] 

或者 

j - i < 2 && str[i] == str[j] (即如果j - i == 1时,为两个字符相等,如果j - i == 0时,为同一个字符) 


这两种情况,我们都将matrix[i][j]设置成true,方便下一次的DP,并且我们可以求出最小的切割次数

cuts[i] = min{cuts[i], cuts[j+1] + 1};  状态转移方程式

这样最后cuts[0] - 1便为 字符串str的最小的切割数!!!!


[java]  view plain copy print ?
  1. public class test {  
  2.     public int minCut(String s) {  
  3.         int min = 0;  
  4.         int len = s.length();  
  5.         boolean[][] matrix = new boolean[len][len];  
  6.         int cuts[] = new int[len+1];  
  7.           
  8.         if (s == null || s.length() == 0)  
  9.             return min;  
  10.         //初始化cuts里面的值为最坏情况的值  
  11.         for (int i=0; i<len; ++i){  
  12.             cuts[i] = len - i;  
  13.         }  
  14.         //dp过程  
  15.         for (int i=len-1; i>=0; --i){  
  16.             for (int j=i; j<len; ++j){  
  17.                 if ((s.charAt(i) == s.charAt(j) && (j-i<2))  
  18.                         || (s.charAt(i) == s.charAt(j) && matrix[i+1][j-1]))  
  19.                 {  
  20.                     matrix[i][j] = true;  
  21.                     cuts[i] = getMinValue(cuts[i], cuts[j+1]+1);  
  22.                 }  
  23.             }  
  24.         }  
  25.         min = cuts[0];  
  26.         return min-1;  
  27.     }  
  28.       
  29.     public int getMinValue(int a, int b){  
  30.         return a > b ? b : a;  
  31.     }  
  32.       
  33.     public static void main(String[] args) {  
  34.         System.out.println(new test().minCut("ababbbabbaba"));  
  35.     }  
  36. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值