Rotate Image,N*N矩阵顺时针旋转90度

public class RotateImage {
	public void rotate(int[][] matrix)
	{
		if(matrix.length == 1 && matrix[0].length == 1)
		{
			return;
		}
		int n = matrix.length;
		for(int i = 0; i < n-1; i ++)
		{
			for(int j = i; j < n-1-i; j ++)
			{
				int temp = matrix[i][j];
				matrix[i][j] = matrix[n-1-j][i];
				matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
				matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
				matrix[j][n-1-i] = temp;
			}
		}
	}
	
	public static void main(String[] args) 
	{
		RotateImage ri = new RotateImage();
		int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
		ri.rotate(a);
		//ri.rotate(a);
		//ri.rotate(a);
		//ri.rotate(a);
		for (int[] is : a) 
		{
			for (int i : is)
			{
				System.out.print(i+" ");
			}
			System.out.println("\n\r");
		}
	}
}

  

posted @ 2016-08-08 17:54  32ddd  阅读(257)  评论(0编辑  收藏  举报