Share my 0 ms C code with O(1) extra space, clear and easy to understand


  • 0
    I
    void rotate(int** matrix, int matrixRowSize, int matrixColSize) {
        int begin = 0, end = matrixRowSize - 1;
        int tmp;
        int i;
        //  rotate from shell to core layer by layer
        while(end > begin) {
            for(i = begin; i < end; ++i) {
                tmp = matrix[begin][i];
                matrix[begin][i] = matrix[end - i + begin][begin];
                matrix[end - i + begin][begin] = matrix[end][end - i + begin];
                matrix[end][end - i + begin]  = matrix[i][end];
                matrix[i][end] = tmp;
            }
            ++begin;
            --end;
        }
    }

Log in to reply
 

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