美文网首页
5. Longest Palindromic Substring

5. Longest Palindromic Substring

作者: wtmxx | 来源:发表于2018-02-01 21:25 被阅读0次
    class Solution {
        public String longestPalindrome(String s) {
            if(s.length()<=1)
                return s;
            String zz = "";
            int ll = 0;
            int len = s.length();
            //0   1   2   3   4   5 
            //b , a , b , b , a , b
            //0 1 2 3 4 5 6 7 8 9 10
            for(int i=0;i<=2*len-2;i++){
                int tmp = (i%2==0)?1:0;
                for(int j=i/2;j>=0&&i-j<len;j--){
                    if(s.charAt(j)==s.charAt(i-j)){
                        tmp = (j==i-j)?tmp:tmp+2;
                        if(tmp>ll){
                            ll = tmp;
                            zz = s.substring(j, i-j+1);
                        }
                    }else{
                        break;
                    }
                }
            }   
            return zz;
        }
    }
    

    相关文章

      网友评论

          本文标题:5. Longest Palindromic Substring

          本文链接:https://www.haomeiwen.com/subject/jgujzxtx.html