【Hot100】LeetCode—5. 最长回文子串

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐++5. 最长回文子串++------题解思路](#⭐5. 最长回文子串——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

题目识别

  • 识别1 :给一个 String 返回最长回文子串

动规五部曲

  • 1- 定义 dp 数组
    • dp[i][j] 代表 区间 [i,j] 是否为回文子串,如果是则为 true
  • 2- 递推公式
    • 只有在 s[i] == s[j] 的情况下,才需要进行递推。分三种情况
      • i == j :若是同一个元素,则 dp[i][j] 肯定为 true
      • ij 相差 1:若是两个相邻元素,则 dp[i][j] 也一定为 true
      • j - i > 1:若是相差两个以上的元素,需要判断 dp[i+1][j-1] 是否为 true
  • 3- 初始化
    • 默认所有 都是为 false
  • 4- 遍历顺序
    • dp[i][j]dp[i+1][j-1] 推导而来,也就是由左下角推导而来,因此
    • i 需要从 s.size() 来遍历

2- 实现

⭐++5. 最长回文子串++------题解思路

java 复制代码
class Solution {
    public String longestPalindrome(String s) {
        // 1. 利用回文子串求解
        // 定义 dp
        int len = s.length();
        boolean[][] dp = new boolean[len][len];

        // 2. 递推公式
        // 只有相等的时候 需要进行递推

        // 3. 初始化
        // 默认都是 false
        String res = "";
        // 4.遍历顺序
        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;
                    }else if(dp[i+1][j-1]){
                        dp[i][j] = true;
                    }
                    if(dp[i][j] && res.length() < j-i+1){
                        res = s.substring(i,j+1);
                    }
                }
            }
        }
        return res;
    }
}

3- ACM 实现

java 复制代码
public class longestPlainDrome {


    public static String longestP(String s){
        String res = "";
        // 1.定义 dp 数组
        int len = s.length();
        boolean[][] dp = new boolean[len][len];

        // 2.递推公式
        // 相等: <=1 则为 true ,否则得看 dp[i+1][j-1] 为 true 才为 true

        // 3. 初始化
        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;
                    }else if(dp[i+1][j-1]){
                        dp[i][j] = true;
                    }
                    if(dp[i][j] && j-i+1 > res.length()){
                        res = s.substring(i,j+1);
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println("输入字符串");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        System.out.println("结果是"+longestP(input));
    }
}
相关推荐
CappuccinoRose1 分钟前
MATLAB学习文档(二十八)
开发语言·学习·算法·matlab
Freedom_my20 分钟前
插入排序算法
数据结构·算法·排序算法
9523623 分钟前
排序-算法
数据结构·算法·排序算法
WongKyunban25 分钟前
插入排序的原理和示例
数据结构·算法·排序算法
flashlight_hi41 分钟前
LeetCode 分类刷题:404. 左叶子之和
javascript·算法·leetcode
小白程序员成长日记2 小时前
2025.11.19 力扣每日一题
算法·leetcode·职场和发展
迈巴赫车主3 小时前
蓝桥杯 20541魔法科考试
java·数据结构·算法·蓝桥杯
star learning white3 小时前
xmC语言8
c语言·开发语言·算法
青小俊3 小时前
【代码随想录c++刷题】-二分查找 移除元素 有序数组的平方 - 第一章 数组 part 01
c++·算法·leetcode
ytttr8734 小时前
基于MATLAB实现晶体共晶凝固模拟
开发语言·算法·matlab