Simple java 0ms solution, in place


  • 0
    M
    public class Solution {
    public void rotate(int[][] matrix) {
        int N = matrix.length;
        
        for(int n = N; n > 1; n--) {
            for(int k = N - n; k < n - 1; k++) {
                int upperLeft = matrix[N-n][k];
                int upperRight = matrix[k][n-1];
                int bottomRight = matrix[n-1][N-k-1];
                int bottomLeft = matrix[N-k-1][N-n];
                
                matrix[k][n-1] = upperLeft;
                matrix[n-1][N-k-1] = upperRight;
                matrix[N-k-1][N-n] = bottomRight;
                matrix[N-n][k] = bottomLeft;
            }
        }
    }
    }

Log in to reply
 

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