My C code. the test-case Input:"abb" Output:"bbc" . But on my own computer the Output is :"bb"


  • 0
    A
    char* longestPalindrome(char* s){
        int len = strlen(s);
        int maxdis = 0;
        int maxdis1 = 0;
        int maxdis2 = 0;
        int locate, locate1, locate2;
        int j, k;
        static char result[1000];
        if(len == 1)
            return s;
        if(len == 2 && s[0] == s[1])
            return s;
        for(int i = 1; i < len; i++){
            for(j = i - 1, k = i + 1; j >= 0 && k < len && s[j] == s[k]; j--, k++);
            if((i-j) > maxdis1){
                maxdis1 = i-j;
                locate1 = j + 1;
            }
        }
        for(int i = 1; i < len; i++){
            for(j = i - 1, k = i; j >= 0 && k < len && s[j] == s[k]; j--, k++);
            if((i-j) > maxdis2){
                maxdis2 = i-j;
                locate2 = j + 1;
            }
        }
        if((2 * maxdis1 + 1) > (2 * maxdis2)){
            maxdis = 2 * maxdis1 + 1;
            locate = locate1;
        }
        else{
            maxdis = 2 * maxdis2;
            locate = locate2;
        }
        memcpy(result, s + locate, maxdis - 2);
        return result;
    }

  • 0
    A

    result[1000] should be initialized.
    memset(result, 0, 1000);


Log in to reply
 

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