Test is right,but is wrong when i submit??? WHY???


  • 0
    A

    class Solution {
    public:

    string longestPalindrome(string s) {
        char *p  = new char[s.length()*2];
        int  ans=0,ansIndex=0,i=0,j=0,huiwenLength=0,k=0,start=0;
    
        for(i=0; i<s.length(); i++){
            p[k++] = '#';
            p[k++] = s[i];
        }
        p[k++] = '#';
        p[k] = '\0';
    
        for(i=0; i<strlen(p); i++){
            huiwenLength = 0;
            for(j=i; j>=0; j--){
                if(p[j] != p[2*i-j])break;
                huiwenLength ++;
            }
            if(ans < huiwenLength) {
                ans = huiwenLength;
                ansIndex = i;
            }
        }
        p[ans+ansIndex] = '\0';
        start = ansIndex - ans  +1;
        while(start--) p ++;
        for(i=1,k=0; p[i]!='\0'; i++,i++)
            p[k++] = p[i];
        p[k] = '\0';
        return  p;
    }
    

    };


Log in to reply
 

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