The output is right but runtime error?


  • 0
    N

    Last executed input: "glzamzp",5.
    My output is "glzpazm", that should be right...here's my code , thanks!!

    char* convert(char* s, int numRows) {
    	int i,j,count=0,index=0,next,len=strlen(s);
        char *tmp=(char *)malloc(len);
    
        if(len==0 || len==1 || numRows==1)
    		return s;
    
        for(i=0,j=0;i<numRows;i++){
            if(i==0 || i==numRows-1){
                while(i+count*(2*numRows-2)<len){
                    tmp[j++]=s[i+count*(2*numRows-2)];
                    count++;
                }
                count=0;
            }
    
            else{
                tmp[j++]=s[i];
                //need loop here
                next=i;
                while(1){
                    next+=( (index%2)?(2*i):2*(numRows-1-i) );
                    if(next>=len)
                        break;
                    tmp[j++]=s[next];
                    index++;
                }
                index=0;
            }
    
        }
        tmp[j]='\0';
        return tmp;
    }

  • 0
    R

    I met the same problem. Who can help me to fix it?
    Submission Result: Wrong Answer
    Output:
    "ABCED"
    Expected:
    "ABCED"


  • 0
    R

    I fixed my problem. I access the data which is out of the input string range . So the results has some invisible characters. I think you would meet the similar problem.


Log in to reply
 

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