Shortest java AC solution


  • 0
    J
        public void rotate(int[][] matrix) {
            transfer(matrix);
            for(int i=0;i<matrix.length;i++){
                for(int j=0;j<matrix[0].length/2;j++)  swap(matrix, i, j, i, matrix[0].length-j-1);
            }
        }
        public void transfer(int[][] matrix){
            for(int i=0;i<matrix.length;i++){
                for(int j=i+1;j<matrix[0].length;j++)   swap(matrix, i,j,j,i);
            }
        }
        public void swap(int[][] matrix, int i1, int j1, int i2, int j2){
            matrix[i1][j1] ^= matrix[i2][j2];
            matrix[i2][j2] ^= matrix[i1][j1];
            matrix[i1][j1] ^= matrix[i2][j2];
        }
    

    First make t(matrix) where (i,j) -> (j,i), then swap to (j, len-i-1) which is equal to 90 degree transfer.


Log in to reply
 

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