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 tR = 0;
int tC = 0;
int dR = matrix.length-1;
int dC = matrix[0].length-1;
while (tR < dR) {
rotateEdge(matrix, tR++, tC++, dR--, dC--);
}
}
private void rotateEdge(int[][] matrix, int tR, int tC, int dR, int dC) {
// TODO Auto-generated method stub
int times = dC - tC;
int tmp = 0;
for (int i = 0; i != times; i++) {
tmp = matrix[tR][tC+i];
matrix[tR][tC+i] = matrix[dR-i][tC];
matrix[dR-i][tC] = matrix[dR][dC-i];
matrix[dR][dC-i] = matrix[tR+i][dC];
matrix[tR+i][dC] = tmp;
}
}
}
2163

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



