原地旋转矩阵(不申请新的内存空间旋转矩阵)

Given  an  image  represented  by  an  NxN  matrix,  where  each  pixel  in  the  imageis 4bytes, write a method to rotate the image by 90 degrees  Can you do this in place?

描述:给定一个N*N的图像,每个位置的像素是4byte,写一个方法用来在原来的空间内旋转图像90度。

思路:我们可以按层来旋转。

void matrixRotation(int [][]a,int n)
{
	
	for(int layer=0;layer<n;layer++)
	{
		int first=layer;
		int last = n-layer-1;
		for(int i=first;i<last;i++)
		{
			int offset=i-first;
			int top =a[first][i];
			a[first][i]=a[last-offset][first];//left->top
			a[last-offset][first]=a[last][last-offset];//bottom->left
			a[last][last-offset]=a[i][last];//right->bottom
			a[i][last]=top;//top->right
		}
	}

	
	
}



posted @ 2012-07-14 19:57  JWMNEU  阅读(325)  评论(0编辑  收藏  举报