【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));
    }
}
相关推荐
oliveira-time3 分钟前
C++ prime plus课后习题-第二章
开发语言·c++·算法
Chase-Hart35 分钟前
【每日一题】LeetCode 7.整数反转(数学)
java·数据结构·算法·leetcode·eclipse
IT枫斗者1 小时前
集合工具类
java·linux·数据库·windows·算法·microsoft
朱皮皮呀1 小时前
排序算法-归并排序
数据结构·算法·排序算法·归并排序
MogulNemenis1 小时前
力扣100题——贪心算法
算法·leetcode·贪心算法
aWty_1 小时前
机器学习--线性回归
python·算法·机器学习·线性回归
我搞slam2 小时前
Cartographer源码理解
算法·slam·cartographer
SEU-WYL4 小时前
基于深度学习的因果发现算法
人工智能·深度学习·算法
Rivieres5 小时前
算法入门-贪心1
java·算法·leetcode·推荐算法
laocooon5238578868 小时前
一个线性筛的多功能组合:筛法求质数+约数个数+约数和
数据结构·c++·算法