Problem: Rotate Image

1.png

2.png

class Solution {
    public void rotate(int[][] matrix) {
        for (int col = 0; col < matrix[0].length; col++){
            for (int row = 0; row < matrix.length/2; row++){
                swap(matrix, row, col, matrix.length-row-1, col);
            }
        }

        for (int i = 0; i < matrix.length; i++){
            for (int j = i; j < matrix[0].length; j++){
                swap(matrix, i,j, j, i);
            }
        }
    }

    private void swap(int[][] matrix, int x1, int y1, int x2, int y2){
        int temp = matrix[x1][y1];
        matrix[x1][y1] = matrix[x2][y2];
        matrix[x2][y2] = temp;
    }
}