A simple java solution


  • 0
    P
    public class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for(int i =0 ;i<n/2;i++){
            for(int j=i;j<n-1-i;j++){
                int temp = matrix[i][j];
                matrix[j][n-i-1] ^= temp;
                temp ^= matrix[j][n-i-1];
                matrix[j][n-i-1] ^= temp;
                matrix[n-i-1][n-j-1] ^= temp;
                temp ^= matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] ^= temp;
                matrix[n-j-1][i] ^= temp;
                temp ^= matrix[n-j-1][i];
                matrix[n-j-1][i] ^= temp;
                matrix[i][j] ^= temp;
                temp ^= matrix[i][j];
                matrix[i][j] ^= temp;
            }
        }
    }
    

    use a mathermatical rule:

    matrix[j][n-i-1] = matrix[i][j]


Log in to reply
 

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