When to 'bb',the error is String index out of range: -1


  • 0
    O

    public class Solution {
    public String longestPalindrome(String s) {
    int i,j,max=0,c=0;
    int length=s.length();
    String string=null;
    int e=0;
    if(s==null||length==0){
    return null;
    }
    if(length<=1){
    return s;
    }
    for(i=0;i<length;i++){
    for(j=0;i+j<length&&i-j>=0;j++){
    if (s.charAt(i-j)!=s.charAt(i+j))
    break;
    c=j<<1+1;
    }
    if (c>max) {
    max=c;
    }
    for(j=0;i+j+1<length&&i-j>=0;j++){
    if (s.charAt(i-j)!=s.charAt(i+j+1)) {
    break;
    }
    c=j<<1+2;
    }
    if (c>max) {
    max=c;
    }
    if((max^0x1)==1){//偶数
    string=s.substring(i-max/2+1,i+max/2);
    }else{//奇数
    string=s.substring(i-(max-1)/2,i+(max-1)/2);
    }
    }
    return string;
    }
    }


Log in to reply
 

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