Do the rotate in-place JAVA


  • 0
    F
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int row = n / 2;
        int col = n / 2;
        if (n % 2 != 0) {
            col++;
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = tmp;
            }
        }
    }

Log in to reply
 

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