Is the testdata has wrong?


  • 0
    S

    By C code

    for the test data

    PAHNAPLSIIGYIR
    3

    my output( finished):

    PAHNAPLSIIGYIR

    the output of the question:

    PAHNAPLSIIGYIR

    but there is some wrong
    is anyone can see where is the wrong??

    char* convert(char* s, int numRows) {
    char* re;
    char* c;
    int i,j,next;
    int fst,sec;
    c = malloc(1);
    re = malloc(strlen(s));
    strcpy(re,"");
    //printf("re:%s slen: %d",re, strlen(s));
    
    
    if(numRows == 1)
        return s;
    else if(numRows == 2)
    {
        i = 0;
        while(i < strlen(s))
        {
           c[0] = s[i];
           strcat(re,c);
           i = i + 2;
        }   
        i = 1;
        while(i < strlen(s))
        {
           c[0] = s[i];
           strcat(re,c);
           i = i + 2;
        }        
    }
    
    
    next = (numRows - 1) * 2 ; //4
    i = 0;
    while(i < strlen(s))
    {
        c[0] = s[i];
        strcat(re,c);
        i = i + next;
    }
    
    for(i=1;i<numRows-1;i++)
    {
        fst = i;
        sec = next - i;
        while(fst < strlen(s))
        {
            c[0] = s[fst];
            strcat(re,c);
            c[0] = s[sec];
            strcat(re,c);
            
            fst = fst + next;
            sec = sec + next;
        }
    }
    
    i = numRows-1;
    while(i < strlen(s))
    {
        c[0] = s[i];
        strcat(re,c);
        i = i + next;
    }    
    return re;
    

    }


  • 0
    T

    What's wrong the expected and the actualy output looks the same, maybe there's an extra space or something at the end. Don't you need to malloc strlen+1 to accomodate for the extra \0 which is not counted by strlen?

    Note: c[0]=x;strcat(re,c); can be simplified to re[r++]=x where int r=0 at the beginning.


Log in to reply
 

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