LeetCode 48 : Rotate Image
Solution to Rotate Image Problem. Here is a nice video which explains the approach.
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Steps :
// 1. Transpose the matrix : means - change row to column
for(int i=0;i< n;i++) {
for(int j=i;j< n;j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
// 2. Reverse the rows
for(int i=0;i<n; i++) {
for(int j=0;j< n/2;j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = tmp;
}
}
}
}