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 length = matrix.length;
for(int i = 0;i<length/2;i++)
{
for(int j = i;j<length-i-1;j++)
{
int t = matrix[i][j];
matrix[i][j] = matrix[length-j-1][i];
matrix[length-j-1][i] = matrix[length-i-1][length-j-1];
matrix[length-i-1][length-j-1] = matrix[j][length-i-1];
matrix[j][length-i-1] = t;
}
}
}
}