Work in linux but runtime error here


  • 0
    L

    char* convert(char* s, int numRows) {

    int length=0;
    int i=0;
    int j,k=0;
    if (numRows==1)
        return s;
    while (s[i]!='\0')
    {
        length++;
        i++;
    }
    printf("%d",length);
    char *s2 = malloc(sizeof(char)*(length+1));
    for (i=0;i<numRows;i++)
    {
        j=0;
        if(i!=0 && i!=numRows-1)
        {
            while(i+j*(2*numRows-2-2*i)<length)
            {
                s2[k++]=s[i+j*(2*numRows-2-2*i)];
                if (i+j*(2*numRows-2)<length)
                    s2[k++]=s[i+j*(2*numRows-2)];
                j++;
            }
        }
        if(i==0 || i==numRows-1)
        {
            while(i+j*(2*numRows-2)<length)
            {
                s2[k++]=s[i+j*(2*numRows-2)];
                j++;
            }
        } 
    }
    s2[k]='\0';
    return s2;
    

    }


  • 0
    X

    are you sure? i ran your code and it give me *** glibc detected *** double free or corruption (!prev): 0x0000000000501010 ***
    Aborted


Log in to reply
 

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