# Why the program has a run time error when the nRow=1????

• ``````char *convert(char *s, int nRows) {
char *run1=s;
char *run2=run1;
len=strlen(s);
char *result;
result=(char*)malloc(sizeof(char)*(len+1));
if(*s==NULL)
{
result[0]='\0';
return result;
}
if(nRows>=len||nRows==1)
{
strcpy(result,s);
return result;
}
for(int i=0;i<nRows;i++)
{
while(1)
{
if(sum1<len)
{
if(sum1!=0)
{
}
}
else
break;
if((sum1+sum2)<len)
{
if(sum2!=0)
{
}
}
else
break;
sum1=sum1*2+sum2;
sum2=sum2*2+sum1;
}
sum1=sum1-2;
sum2=sum2+2;
}
result[len]='\0';
return result;
``````

}

• Check all the boundary conditions. It's not necessary that it is failing on nRows=1 when you get a RE.

BTW, When nRows=1, no need to do computation as you directly return s.

• Thank you for your reply.But there is still having something wrong;
when i change
if(nRows>=len)
{
strcpy(result,s);
return result;
}
to
if(nRows>=len||nRows==1)
{
return s;
}
there has Last executed input: "AB", 1
Can you tell me the exact place that i should change!please!!

• Don't go with the test case of LC when there is a RE. There seems a bug in the logic you used. Before submitting on LC, run on your machine. The basic test case of "PAYPALISHIRING", 3 is not giving correct output.

What I analysed is you are incrementing the index by 2rows -2 each time for every index. This is not right in all cases.

The logic goes something like this:

1. if(nRows>=len || nRows==1 || len==0) simply return s;

2. For 0th row and nRows-1th row, increment index by 2rows -2 by checking if it falls within the length of the string.

3. Now if the row(say i) is not the first or last row, do below

For each index k starting from 1 and below index < length:

If index is even, increment by 2i
else if index is odd, increment by 2rows - 2 - 2i

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