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

来源:互联网 发布:中国基尼系数数据 编辑:程序博客网 时间:2024/04/29 14:53

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 -> topmatrix[first][i] = matrix[last-offset][first];//bottom -> leftmatrix[last-offset][first] = matrix[last][last -  offset];//right -> bottommatrix[last][last - offset] = matrix[i][last];//top -> rightmatrix[i][last] = top;}}}



0 0
原创粉丝点击