Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
int[][] A = new int[n][n];
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < n; j++)
A[j][n - 1 - i] = matrix[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
matrix[i][j]=A[i][j];
return;
}
}
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
int[][] A = new int[n][n];
for (int i = n - 1; i >= 0; i--)
for (int j = 0; j < n; j++)
A[j][n - 1 - i] = matrix[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
matrix[i][j]=A[i][j];
return;
}
}