Java In-Place Straight Forward Solution


  • 0
    J

    public class Solution {

    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int k = 0; k < n; k++) {
            for (int i = k; i < n - k - 1; i++) {
                swap(matrix, k, n - i - 1, i, k);
                swap(matrix, i, k, n - k - 1, i);
                swap(matrix, n - k - 1, i, n - i - 1, n - k - 1);
            }
        }
    }
    public void swap(int[][] matrix, int a, int b, int c, int d) {
        if (matrix[a][b] == matrix[c][d]) return;
        matrix[a][b] ^= matrix[c][d];
        matrix[c][d] ^= matrix[a][b];
        matrix[a][b] ^= matrix[c][d];
        return;
    }
    

    }


Log in to reply
 

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