IncredibleThings

导航

LeetCode-Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

这道题是比较常考的题目,求回文子串,一般有两种方法。 第一种方法比较直接,实现起来比较容易理解。基本思路是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙)往两边同时进行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。对于每个中心往两边扫描的复杂度为O(n),所以时间复杂度为O((2*n-1)*n)=O(n^2),空间复杂度为O(1),代码如下:

 

public class Solution {
    public String longestPalindrome(String s) {
        if(s==null || s.length()==0){
            return "";
        }
        int maxLen=0;
        String res="";
        for(int i=0; i<s.length(); i++){
            if(i==0){
                maxLen=1;
                res=Character.toString(s.charAt(i));
            }
            else{
                //devided into two type
                int l1=i-1;
                int r1=i;
                String s1=getPalindrome(s, l1, r1);        
                int l2=i-1;
                int r2=i+1;
                String s2=getPalindrome(s, l2, r2);
                if(s1.length()>s2.length()){
                    if(s1.length()>maxLen){
                        maxLen=s1.length();
                        res=s1;
                    }
                }
                else{
                    if(s2.length()>maxLen){
                        maxLen=s2.length();
                        res=s2;
                    }
                }
            }
        }
        return res;
    }
    
    public String getPalindrome(String s, int left, int right){
        boolean hasP=false;
        while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){
            hasP=true;
            left--;
            right++;
        }
        if(hasP){
            return s.substring(left+1,right);
        } 
        else{
            return "";
        }
    }
}

 二刷:

class Solution {
    public String longestPalindrome(String s) {
        if(s == null || s.length() == 0){
            return "";
        }
        String res = "";
        for(int i = 0; i < s.length(); i++){
            int window = 0;
            int l = i, r = i;
            String temp = "";
            //odd case
            while(i-window >= 0 && i+window < s.length() && (s.charAt(i-window) == s.charAt(i+window))){
                l = i-window;
                r = i+window;
                window++;
            }
            temp = s.substring(l,r+1);
            if(temp.length() > res.length()){
                res = temp;
            }
            l = i;
            r= i;
            window = 0;
            //even case
            while(i < s.length() - 1 && i-window >= 0 && i+1+window < s.length() && (s.charAt(i-window) == s.charAt(i+1+window))){
                l = i - window;
                r = i+window+1;
                window++;
            }
            temp = s.substring(l, r+1);
            if(temp.length() > res.length()){
                res = temp;
            }
        }
        return res;
    }
}

 

posted on 2016-07-20 23:36  IncredibleThings  阅读(144)  评论(0编辑  收藏  举报