题目
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?
java实现
public void rotate(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
int[][] copy = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
copy[j][row - 1 - i] = matrix[i][j];
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = copy[i][j];
}
}
}