My Simple C Solution 0ms


  • 0
    A
    int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
        int i, j, k, m, n, p=0, s;
        s = matrixRowSize*matrixColSize;
        int *r = (int *)malloc(sizeof(int) * s);
        for(i=0; i<(matrixRowSize+1)/2&&p<s; i++) {
            for(j=i; j<matrixColSize-i&&p<s; j++) {
                r[p++] = matrix[i][j];
            }
            for(k=i+1; k<matrixRowSize-i&&p<s; k++) {
                r[p++] = matrix[k][j-1];
            }
            for(m=j-2; m>=i&&p<s; m--) {
                r[p++] = matrix[k-1][m]; 
            }
            for(n=matrixRowSize-i-2; n>=i+1&&p<s; n--) {
                r[p++] = matrix[n][m+1]; 
            }
        }
        return r;
    }

Log in to reply
 

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