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?
一刷没ac
解题思路:反转两次。
public class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
for(int i = 0; i < matrix.length; i++){
for(int j = i; j < matrix[0].length; j++){
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length/2; j++){
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[0].length-1-j];
matrix[i][matrix[0].length-1-j] = tmp;
}
}
}
}