My 7ms solution


  • 1
    M

    pick up the sequence by period of T = 2*(numRows - 1)

    char* convert(char* s, int numRows) {
            int n = strlen(s);
            char *result = NULL;
            int k = 0;
            
            if (numRows == 1)
                return strdup(s);
                
            result = (char*)malloc(n+1);
            memset(result, 0, n+1);
                
            for(int i = 1; i <= numRows; ++i){
                int T = 2*(numRows - 1);
                int b = 2*(numRows - i);
                for(int j = i - 1 ; j < n; j += T){
                    
                    result[k++] = s[j];
                    if(i != numRows && i != 1 && j + b < n){
                        result[k++] = s[j + b];
                    }
                }
            }
    
            return result; 
    }

Log in to reply
 

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