Share my Java Solution (192ms)


  • 4
    B

    public class Solution {

    public void rotate(int[][] matrix) {
    
        if(matrix.length == 0 || matrix.length == 1){
            return;
        }
    
        int temp = 0;
        int n = matrix.length - 1;
    
        for(int i = 0; i < matrix.length - 1; i++){
            for(int j = 0; j + i < n; j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[n-j][n-i];
                matrix[n-j][n-i] = temp;
            }
        }
        
        for(int i = 0; i< matrix.length / 2; i++){
            for(int j = 0; j < matrix[0].length; j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[n-i][j];
                matrix[n-i][j] = temp;
            }
        }
    }
    

    }

    Algorithm: m[i][j] <-> m[n-j][n-i]
    //First swap diagonally
    m[i][j] <-> matrix[n-i][j]
    // Second swap vertically


Log in to reply
 

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