Why Runtime Error?But result is correct.


  • 0
    W
    char* convert(char* s, int numRows) {
    unsigned int s_num=numRows*2-2;
    unsigned int s_length=strlen(s);
    if(s_length==0||s_length==1||numRows==1)return s;
    unsigned int i,i_com,i_next,row;
    char *p;
    char *result=(char *)malloc(s_length*sizeof(char));
    
    char *go=result;
    for(row=0;row<numRows;row++)
    {
        i_com=row<<1;
        i=s_num-i_com;
        p=s+row;
        if(i_com==s_num||i==s_num)
        {
            do{
                *go=*p;
                ++go;
                p+=s_num;
            }while(*p);
        }else
        {
            i_next=i;
            do{
                *go=*p;
                ++go;
                p+=i_next;
                i_next=s_num-i_next;
            }while(*p);
        }
    }
    return result;
    

    }


  • 0
    C

    I think you need to strcpy result to the input char *s.


Log in to reply
 

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