My in-space solution in Java

  • 0
    public class Solution {
     public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int i=0; i< (n/2); i++)
            rotateWithRowNum(matrix, i, n);
    private void rotateWithRowNum(int[][] matrix, int rowNum, int n) {
        int swap;
        for (int i=rowNum; i<n-1-rowNum; i++) {
            swap = matrix[rowNum][i];
            matrix[rowNum][i] = matrix[n-1-i][rowNum];
            matrix[n-1-i][rowNum] = matrix[n-1-rowNum][n-1-i];
            matrix[n-1-rowNum][n-1-i] = matrix[i][n-1-rowNum];
            matrix[i][n-1-rowNum] = swap;


Log in to reply

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