My Java Solution


  • 0
    S

    '''
    public class Solution {
    public String longestPalindrome(String s) {
    if(s==null||s.length()==0) return "";
    char[] chs=s.toCharArray();
    int n=chs.length;
    int left=0,max=0;
    for(int i=0;i<n;i++){
    for(int j=0;i-j>=0&&i+j<n&&chs[i-j]==chs[i+j];j++){
    if((i+j)-(i-j)+1>max){
    max=(i+j)-(i-j)+1;
    left=i-j;
    }
    }
    for(int j=1;i-j+1>=0&&i+j<n&&chs[i-j+1]==chs[i+j];j++){
    if((i+j)-(i-j+1)+1>max){
    max=(i+j)-(i-j+1)+1;
    left=i-j+1;
    }
    }
    }
    return s.substring(left,left+max);
    }
    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.