码迷,mamicode.com
首页 > 其他好文 > 详细

旋转一个矩阵

时间:2019-03-19 15:02:26      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:which   other   中心   ++i   --   旋转   图片   get   mod   

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.

思路:转置在中心对称线交换

 

技术图片

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int row=matrix.size();
int col=matrix[0].size();
for(int i=0;i<row;++i){
for(int j=0;j<i;++j){
swap(matrix[i][j],matrix[j][i]);
}
}
for(int i=0;i<row;++i){
int l=0,r=row-1;
while(l<r){
swap(matrix[i][l],matrix[i][r]);
l++;r--;
}
}
}
};

旋转一个矩阵

标签:which   other   中心   ++i   --   旋转   图片   get   mod   

原文地址:https://www.cnblogs.com/zzas0/p/10558608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!