Why my code is correct on my computer but wrong on LeetCode


  • 0
    Y

    Here is my code.

    char* longestPalindrome(char* s) {
    int i,j;
    int start,end;
    int len;
    int map;
    char res;
    int count = 0;
    map = (int
    )malloc(sizeof(int
    )1000);
    start = end = 0;
    for( i = 0; i < 1000;i++ ){
    (map+i) = (int)malloc(sizeof(int)1000);
    }
    for( i = 0; s[i] != '\0' ; i++ ){
    for( j = i; j >= 0; j-- ){
    if( i == j ){
    map[i][j] = 1;
    }else if( i == j+1){
    map[i][j] = (s[i]==s[j]);
    }else if(map[i+1][j-1] && s[i]==s[j]){
    map[i][j] = 1;
    }else{
    map[i][j] = 0;
    }
    if(map[i][j] && ((i-j)>(end-start))){
    start = j;
    end = i;
    //printf("%d,%d\n",start,end);
    }
    }
    }
    count = end-start+1;
    res = (char
    )malloc(sizeof(char)
    (count+1));
    for( i = 0; i < count; i++ ){
    res[i] = s[i+start];
    }
    res[i] = '\0';
    return res;
    }

    int main()
    {
    char *s = "babad";
    char *p;
    p = longestPalindrome(s);
    printf("%s",p);
    }

    It is correct on my computer.
    But when it comes to LeetCode, it is wrong, why?


Log in to reply
 

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