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 len=matrix[0].length;
for(int i=0;i<len/2;i++){
int first=i;
int last=len-i-1;
for(int j=first;j<last;j++){
int tmp=matrix[first][j];
matrix[first][j]=matrix[last-j+first][first];//left to up
matrix[last-j+first][first]=matrix[last][last-j+first];//down to left
matrix[last][last-j+first]=matrix[j][last];//right to down
matrix[j][last]=tmp;//up to right
}
}
}
}
1553

被折叠的 条评论
为什么被折叠?



