class Solution {
    public void rotate(int[][] matrix) {
        if(matrix.length == 0 || matrix[0].length == 0) return;
        for(int row = 0;row < matrix.length; ++row) {
            for(int col = row; col < matrix[0].length; ++col) {
            //note here to do transpose, col have to start with row else we will return the original matrix
                int temp = matrix[row][col];
                matrix[row][col] = matrix[col][row];
                matrix[col][row] = temp;
            }
        }
        for(int row = 0; row< matrix.length; ++row) {
            for(int col = 0; col<matrix[0].length/2; ++col) {
                int temp = matrix[row][col];
                matrix[row][col] = matrix[row][matrix[0].length-col-1];
                matrix[row][matrix[0].length-col-1] = temp;
            }
        }

    }
}

results matching ""

    No results matching ""