代码随想录算法训练营第五十二天 | 647. 回文子串、516.最长回文子序列、动态规划总结篇、复习

647. 回文子串

题目链接:https://leetcode.cn/problems/palindromic-substrings/
文档讲解:https://programmercarl.com/0647.%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.html
视频讲解:https://www.bilibili.com/video/BV17G4y1y7z9/

思路

  • 确定dp数组以及下标的含义:
  • 确定递推公式:当s[i] == s[j]时,
    • 情况一:i == j,比如a。递推公式为dp[i][j] = true
    • 情况二:j - i < 1,比如aa。递推公式为dp[i][j] = true
    • 情况三:j - i > 1。递推公式为if (dp[i + 1][j - 1] == true) dp[i][j] = true
  • dp数组如何初始化:初始化为false
  • 确定遍历顺序:从左下到右上。所以i从最大值开始递减,由于j大于等于i,所以j从i开始递增。
  • 打印dp数组,用于debug

代码

class Solution {
    public int countSubstrings(String s) {
        int len = s.length();
        boolean[][] dp = new boolean[len][len];
        for (boolean[] row : dp)  Arrays.fill(row, false);
        int res = 0;
        for (int i = len - 1; i >= 0; i--) {
            for (int j = i; j < len; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    if (j - i <= 1) {
                        dp[i][j] = true;
                        res++;
                    } else {
                        if (dp[i + 1][j - 1] == true) {
                            dp[i][j] = true;
                            res++;
                        }
                    }
                }
            }
        }
        return res;
    }
}

分析:时间复杂度:O(n2),空间复杂度:O(n2)。

516.最长回文子序列

题目链接:https://leetcode.cn/problems/longest-palindromic-subsequence/
文档讲解:https://programmercarl.com/0516.%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E5%BA%8F…
视频讲解:https://www.bilibili.com/video/BV1d8411K7W6/

思路

  • 确定dp数组以及下标的含义:[i, j]子串最长回文子序列长度为dp[i][j]
  • 确定递推公式:
  • dp数组如何初始化:左上到右下的对角线初始化成1。
  • 确定遍历顺序:左下到右上。
  • 打印dp数组,用于debug

代码

class Solution {
    public int longestPalindromeSubseq(String s) {
        int len = s.length();
        int[][] dp = new int[len][len];
        for (int i = len - 1; i >= 0; i--) {
            dp[i][i] = 1;// 初始化可以和递推合并到一个循环中
            for (int j = i + 1; j < len; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                }
                else dp[i][j] = Math.max(dp[i + 1][j] , dp[i][j - 1]);
            }
        }
        return dp[0][len - 1];
    }
}

分析:时间复杂度:O(n2),空间复杂度:O(n2)。

动态规划部分总结

文档讲解:https://programmercarl.com/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E6%80%BB%E7%BB%93%E7%AF…

复习字符串

344.反转字符串
541. 反转字符串II
卡码网:54.替换数字

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最长回文子串是指在一个字符串中最长回文子序列回文是指正着读和倒着读都一样的字符串。动态规划是解决最长回文子串问题的一种常用方法。动态规划的思想是将问题分解成子问题,通过求解子问题的最优解来得到原问题的最优解。在最长回文子串问题中,我们可以使用一个二维数组dp[i][j]来表示从i到j的子串是否为回文子串。如果dp[i][j]为true,则表示从i到j的子串回文子串,否则不是。我们可以通过以下步骤来求解最长回文子串: 1. 初始化dp数组,将所有dp[i][i]都设置为true,表示单个字符是回文子串。 2. 遍历字符串s,从长度为2的子串开始,依次判断每个子串是否为回文子串。如果是,则将dp[i][j]设置为true。 3. 在遍历的过程中,记录最长回文子串的长度和起始位置。 4. 最后,通过起始位置和长度来截取最长回文子串。 下面是一个示例代码,可以帮助你更好地理解动态规划求解最长回文子串的过程: class Solution { public: string longestPalindrome(string s) { int len=s.size(); if(len<2) return s; bool dp[len][len];//布尔型,dp[i][j]表示从i到j是否构成回文 int max_count=1;//最大字串的长度 int start=0;//最长字串的起始位置 for(int j=0;j<len;j++) { for(int i=0;i<j;i++) { if(s[i]!=s[j]) dp[i][j]=false; else if((j-i)<3)//(j-1)-(i+1)+1<2表示dp[i][j]的最大字串长度为1 dp[i][j]=true; else { dp[i][j]=dp[i+1][j-1]; } if((j-i+1)>max_count&&dp[i][j]) { max_count=j-i+1; start=i; } } } return s.substr(start,max_count);//截取字符串 } };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值