题目:
* 1.6给定一幅由N*N矩阵表示的图像,其中每个像素大小为4字节,编写一个方法,将图像旋转90度。不占用额外内存空间能否做到?
代码:
/*
* 1.6给定一幅由N*N矩阵表示的图像,其中每个像素大小为4字节,编写一个方法,将图像旋转90度。
* 不占用额外内存空间能否做到?
*/
package mainshijindian;
public class ImageRotate {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
int n=3;
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 = a[first][i]; //存储上边
a[first][i]=a[last-offset][first]; //左到上
a[last-offset][first] = a[last][last-offset]; //下到左
a[last][last-offset] = a[i][last]; //右到下
a[i][last]=top; //上到右
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println(" ");
}
}
}