首页 > 其他 > 详细

Rotate Image

时间:2014-02-27 00:41:13      阅读:417      评论:0      收藏:0      [点我收藏+]

题目原型:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

基本思路:

题目意思就是把图片(矩阵)顺时针旋转90度,分析得,先交换i,j即:A[i][j]与A[j][i]交换,然后逆置每行。

	public void rotate(int[][] matrix)
	{
		int i = 0;
		int j = 0;
		int k = 0;
		int temp = 0;
		
		for(i = 0;i<matrix.length;i++)
		{
			for(j=i;j<matrix[0].length;j++)
			{
				temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
			
		}
		
		for(i=0;i<matrix.length;i++)
		{
			for(j=0,k=matrix[0].length-1;j<k;j++,k--)
			{
				temp = matrix[i][j];
				matrix[i][j] = matrix[i][k];
				matrix[i][k] = temp;
			}
		}
	}



Rotate Image,布布扣,bubuko.com

Rotate Image

原文:http://blog.csdn.net/cow__sky/article/details/19963973

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!