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

Myth丶恋晨 2022-08-26 08:04 68阅读 0赞

题目:

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

  1. [
  2. ["aa","b"],
  3. ["a","a","b"]
  4. ]

分析:

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

解题思路:(跟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代码:

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

题目:(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的最小的切割数!!!!

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

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9yZW55dWdhbmcuYmxvZy5jc2RuLm5ldA_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读