48. Rotate Image

问题描述
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
这里写图片描述
Example 2:
这里写图片描述

问题分析
该问题给出了一个n*n的二维矩阵来表示一幅图形,然后要求在原矩阵上将图片旋转90度,并且题目规定了不能申请另外的矩阵来进行旋转,要在原矩阵上进行旋转,所以我们首先要找到旋转后的矩阵数值与与原矩阵数值最表对应的关系,由上图我们可以看到
A[0][0] -> A[0][3]
A[1][0] -> A[0][2]
A[0][1] -> A[1][3]
A[2][0] -> A[0][1]
A[0][2] -> A[2][3]
A[3][0] -> A[0][0]
A[0][3] -> A[3][3]
由此可得:对于n * n 的2维矩阵,旋转变换关系为:
A[i][j] -> A[j][n-1-i]
代码展示

int i,j,temp;  
        int n=matrix.size();  
        for(i = 0;i < n/2;++i) {  
            for (j = i;j < n-1-i;++j) {  
                temp = matrix[j][n-i-1];  
                matrix[j][n-i-1] = matrix[i][j];  
                matrix[i][j] = matrix[n-j-1][i];  
                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];  
                matrix[n-i-1][n-j-1] = temp;  
            }//for  
        }//for 

运行结果展示
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值