画解算法:5. 最长回文子串

2019-08-08  本文已影响0人  DeppWang

题目链接

https://leetcode-cn.com/problems/longest-palindromic-substring/

题目描述

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1:

输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。

示例 2:

输入: "cbbd"
输出: "bb"

解题方案

思路

代码 1:最长公共子串

class Solution {
    public String longestPalindrome(String s) {
        if (s.equals("")) {
            return "";
        }
        int length = s.length();
        String reversal = new StringBuffer(s).reverse().toString(); // 反转字符串
        int[][] cell = new int[length][length];
        int maxLen = 0; // 最长公共子串长度
        int maxEnd = 0; // 最长公共子串结束位置
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if (reversal.charAt(i) == s.charAt(j)) {
                    if (i == 0 || j == 0) {
                        cell[i][j] = 1;
                    } else {
                        cell[i][j] = cell[i - 1][j - 1] + 1; // 公共子串长度
                    }
                } else {
                    cell[i][j] = 0;
                }
                if (cell[i][j] > maxLen) {
                    maxLen = cell[i][j];
                    maxEnd = j;
                }
            }
        }
        return s.substring(maxEnd + 1 - maxLen, maxEnd + 1);
    }
}

画解 1

1-1.png
2-1.png
3-1.png

代码 2:最长回文子串

class Solution {
    public String longestPalindrome(String s) {
        if (s.equals("")) {
            return "";
        }
        int length = s.length();
        String reversal = new StringBuffer(s).reverse().toString(); // 反转字符串
        int[][] cell = new int[length][length];
        int maxLen = 0; // 最长回文子串长度
        int maxEnd = 0; // 最长回文子串结束位置
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if (reversal.charAt(i) == s.charAt(j)) {
                    if (i == 0 || j == 0) {
                        cell[i][j] = 1;
                    } else {
                        cell[i][j] = cell[i - 1][j - 1] + 1;
                    }
                }
                /**************修改的地方***************************/
                // 可为空,不用置为0,减少运行时间
//                else {
//                    cell[i][j] = 0;
//                }
                /**************************************************/
                if (cell[i][j] > maxLen) {
                    /**************修改的地方***************************/
                    int beforeIndex = length - 1 - i; // 反向子串末尾字符的原始索引
                    int firstIndex =  j - cell[i][j] + 1; // 子串的首字符索引
                    if (beforeIndex == firstIndex) { 
                        maxLen = cell[i][j];
                        maxEnd = j;
                    }
                    /**************************************************/
                }
            }
        }
        return s.substring(maxEnd + 1 - maxLen, maxEnd + 1);
    }
}

画解 2

1-2.png
2-2(2).png
1.png
2.png

测试用例

描述 1 2 3 4 5
输入 "" "a" "abac" "abcda" "aacdecaa"
输出 "" "a" "aba" "a" "aa"
上一篇下一篇

猜你喜欢

热点阅读