leetcode雕虫小技medium 5. Longest Palindromic Substring

题干:https://leetcode.com/problems/longest-palindromic-substring/

分析:

这题据说有个O(n)的解法,我自己写的O(n^2)的解法不是最优,但是毕竟能过OJ,所以这里就贴上来了,思路是, 分别考虑单数回文和双数回文的情况,从每个中心点开始尝试,向两侧扩张,扩张到回文极限即止, 返回各个中心点情况下的最长回文。

代码:

package com.example.demo.leetcode;

public class LongestPalindromicSubstring {
    public String longestPalindrome(String s) {
        int max = Integer.MIN_VALUE;
        String ret = "";
        for(int i=0;i=0 && right<=s.length()-1){
                if(s.charAt(left)==s.charAt(right)){
                    ret = s.substring(left, right+1);
                    i++;
                }else{
                    break;
                }
            }else{
                break;
            }
        }

        return ret;
    }

    private String doubleExpand2Max(String s, int doubleCoreStart){
        int i=1;
        int left = doubleCoreStart;
        int right = doubleCoreStart+1;
        if(s.charAt(doubleCoreStart)!=s.charAt(doubleCoreStart+1)){
            return "";
        }
        String ret = s.substring(doubleCoreStart, doubleCoreStart+2);
        while(true){
            left=doubleCoreStart-i;
            right=doubleCoreStart+1+i;
            if(left>=0 && right<=s.length()-1){
                if(s.charAt(left)==s.charAt(right)){
                    ret = s.substring(left, right+1);
                    i++;
                }else{
                    break;
                }
            }else{
                break;
            }
        }

        return ret;
    }

    int dp(String input, int index, int[] memtable){
        if(memtable[index]!=-1){
            return memtable[index];
        }
        if(index==0){
            memtable[index]=1;
            return 1;
        }
        if(isPalindrome(input, index-dp(input, index-1, memtable)-1, index)){
            int val = dp(input, index-1, memtable)+2;
            memtable[index]=val;
            return val;
        }else if(isPalindrome(input, index-dp(input,index-1, memtable), index)){
            int val = dp(input, index-1, memtable)+1;
            memtable[index]=val;
            return val;
        }else{
            int val = consecutiveCnt(input, index);
            memtable[index]=val;
            return val;
        }
    }

    public int consecutiveCnt(String input, int index){
        char o = input.charAt(index);
        int cnt = 1;
        for(int j=index-1;j>=0;j--){
            if(input.charAt(j)==o){
                cnt++;
            }else{
                break;
            }
        }
        return cnt;
    }

    public boolean isPalindrome(String input, int startIndex, int endIndex){
        if(startIndex<0){
            return false;
        }
        int i= startIndex;
        int j = endIndex;
        while(j>=i){
            if(input.charAt(i++)==input.charAt(j--)){
                continue;
            }else{
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        LongestPalindromicSubstring demo = new LongestPalindromicSubstring();
        String ret = demo.longestPalindrome("cbbd");

        System.out.println(ret);
    }
}

 

你可能感兴趣的:(leetcode,算法)