LeetCode刷题记录:5. 最长回文子串-Longest Palindromic Substring

LeetCode刷题记录:5. 最长回文子串-Longest Palindromic Substring

题目

给你一个字符串 s,找到 s 中最长的回文子串。

示例 1

输入:s = “babad”
输出:“bab”
解释:“aba” 同样是符合题意的答案。

示例 2

输入:s = “cbbd”
输出:“bb”

示例 3

输入:s = “a”
输出:“a”

示例 4

输入:s = “ac”
输出:“a”

提示

  • 1 <= s.length <= 1000
  • s 仅由数字和英文字母(大写和/或小写)组成

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

解题思路

暴力求解:即判断所有字串是不是回文串,然后求出最长字串,即可得出结果。字串规模是 O(n^2) ,而求解字符串是不是回文串则还须要一定量级的时间复杂度。
动态规划:判断长度为1或2的字符串是否为回文串十分简单。判断 s(i,j) 是不是回文串只需要判断s(i+1,j-1) 是不是回文串以及 s.charAt(i) == s.charAt(j)。这样可以覆盖到s的所有子串。
中心扩散:思考上述动态规划算法,若 s(i,j) 不是回文串,那么我们也不需要判断 s(i-1,j+1) 是不是回文串了。所以仅需根据s的最小字串(即长度为1或2的情况)向字串两边扩散,寻找最长情况即可。
Manacher 算法:TODO

java实现

个人实现(中心扩散法)

public class Solution {
    public static String longestPalindrome(String s) {
        if(s.length()==0){
            return "";
        }
        int place=0;
        int maxValue=1;
        boolean centerFlag=true;
        for(int i=0;i<s.length();i++){
            int checkWithCenter=maxPalindromeLengthWithCenter(i,s);
            if(checkWithCenter>maxValue){
                place=i;
                maxValue=checkWithCenter;
                centerFlag=true;
            }
            int checkWithoutCenter=maxPalindromeLengthWithoutCenter(i,s);
            if(checkWithoutCenter>maxValue){
                place=i;
                maxValue=checkWithoutCenter;
                centerFlag=false;
            }
        }
        return centerFlag?s.substring(place-maxValue/2,place+maxValue/2+1):s.substring(place-maxValue/2+1,place+maxValue/2+1);
    }

    public static int maxPalindromeLengthWithCenter(int i,String s){
        int result=1;
        int j=1;
        while(i-j>=0&&i+j<s.length()&&s.charAt(i-j)==s.charAt(i+j)){
            j++;
            result+=2;
        }
        return result;
    }

    public static int maxPalindromeLengthWithoutCenter(int i,String s){
        int result=0;
        int j=1;
        while(i-j+1>=0&&i+j<s.length()&&s.charAt(i-j+1)==s.charAt(i+j)){
            j++;
            result+=2;
        }
        return result;
    }
}

官方答案(Manacher 算法)

class Solution {
    public String longestPalindrome(String s) {
        int start = 0, end = -1;
        StringBuffer t = new StringBuffer("#");
        for (int i = 0; i < s.length(); ++i) {
            t.append(s.charAt(i));
            t.append('#');
        }
        t.append('#');
        s = t.toString();

        List<Integer> arm_len = new ArrayList<Integer>();
        int right = -1, j = -1;
        for (int i = 0; i < s.length(); ++i) {
            int cur_arm_len;
            if (right >= i) {
                int i_sym = j * 2 - i;
                int min_arm_len = Math.min(arm_len.get(i_sym), right - i);
                cur_arm_len = expand(s, i - min_arm_len, i + min_arm_len);
            } else {
                cur_arm_len = expand(s, i, i);
            }
            arm_len.add(cur_arm_len);
            if (i + cur_arm_len > right) {
                j = i;
                right = i + cur_arm_len;
            }
            if (cur_arm_len * 2 + 1 > end - start) {
                start = i - cur_arm_len;
                end = i + cur_arm_len;
            }
        }

        StringBuffer ans = new StringBuffer();
        for (int i = start; i <= end; ++i) {
            if (s.charAt(i) != '#') {
                ans.append(s.charAt(i));
            }
        }
        return ans.toString();
    }

    public int expand(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            --left;
            ++right;
        }
        return (right - left - 2) / 2;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值