My 8ms C solution


  • 1
    M
    char* convert(char* s, int numRows) {
        int strLen = strlen(s), counter = 0;
        char *ret = malloc((strLen+1)*(sizeof(char)));
        if(numRows == 1 || numRows >= strLen) {
            for(int i = 0; i < strLen; i++) {
                ret[i] = s[i];}}
        else {
            for(int i = 1; i <= numRows; i++) {
                for(int j = i; j <= strLen; j += 2*numRows-2) {
                    ret[counter++] = s[j-1];
                    if(1 != i &&  i != numRows && j+2*(numRows-i) <= strLen) {
                        ret[counter++] = s[j+2*(numRows-i)-1];}}}}
        ret[strLen] = '\0';
        return ret;}

Log in to reply
 

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