您的位置:首页 > 其它

顺时针旋转图片90度(算法)

2015-03-11 13:10 316 查看
Give an image represented by an NxN matrix ,where each pixel in the image is 4 bytes,write a method to rotate the image by 90 degrees .Can you do this in place?

Once the exterior elements are rotated ,we then rotate the interior region's edges.

public static void rotate(int [][] matrix,int n)
{
for(int layer = 0; layer < n/2; layer ++)
{
int first = layer;
int last = n-1-layer;
for(int i = first; i < last; i++)
{
int offset = i - first;
int top = matrix[first][i];
//left -> top
matrix[first][i] = matrix[last-offset][first];
//bottom -> left
matrix[last-offset][first] = matrix[last][last -  offset];
//right -> bottom
matrix[last][last - offset] = matrix[i][last];
//top -> right
matrix[i][last] = top;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: