标签:
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?
Subscribe to see which companies asked this question
分析:
通过实际数据分析,通过两个步骤的元素交换可实现目标:
按照主对角线,将对称元素交换
按照列,将对称列元素全部交换
即可达到,使得二维矩阵,本地旋转90个角度。
代码如下:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n=matrix.size();
//一,对角线对换
for(int i=0;i<n-1;i++)//列
{
for(int j=0;j<n-1-i;j++)//行
{
int tmp=matrix[j][i];
matrix[j][i]=matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j]=tmp;
}
}
//二,水平线对换
for(int i=0;i<n;i++)//列
{
for(int j=0;j<n/2;j++)//行
{
int tmp=matrix[j][i];
matrix[j][i]=matrix[n-1-j][i];
matrix[n-1-j][i]=tmp;
}
}
}
};注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/51588641
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
<LeetCode OJ> 48. Rotate Image
标签:
原文地址:http://blog.csdn.net/ebowtang/article/details/51588641