My 6ms c solution


  • 0
    L
    char *convert(char *s, int nRows) {
       
        int len=strlen(s);
        char * res=NULL;
        res=(char *)malloc(len+1);
         if(nRows<=1||strlen(s)<nRows) {strcpy(res, s); return res;}
        else if (len<= 0) { 
        res[0] = '\0';
        return res;
        }
        else if(res!=NULL){
        int i,j=0,count=0;
        for( i=0;i<nRows;i++){
          for(j=i;j<len;j+=2*nRows-2){
                    res[count++]=s[j];
                    if(i>0&&i<nRows-1&&(j+2*(nRows-i-1))<len)
                        res[count++]=s[j+2*(nRows-i-1)];
                }
        }
        res[count]='\0';
        return res;
        }
    }

Log in to reply
 

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