您好,欢迎来到刀刀网。
搜索
您的当前位置:首页旋转一个矩阵

旋转一个矩阵

来源:刀刀网

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 , 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--;
}
}
}
};

转载于:https://www.cnblogs.com/zzas0/p/10558608.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务