9ms c code O(n)/O(n) (space/computation)


  • 0
    1
    char* convert(char* s, int numRows) {
      int m = numRows-2, rows, idx=0, slen=strlen(s), resIdx=0, skip=0;
      
      char * res = (char *)malloc(slen+1);
      if(!res) return res;
      bzero(res, slen+1);
      if(!numRows || numRows==1) {strncpy(res, s, slen);return res;}
      for(rows=0;rows<numRows;rows++) {
        skip=numRows-(2*rows)+m;
        idx=rows;
        res[resIdx++]=s[idx];
    
        while((idx+=skip) < slen){
          if(skip) res[resIdx++]=s[idx];
          skip=(numRows-skip+m);
        }
      }
      res[slen]='\0';
      return res;
    }
    

Log in to reply
 

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