题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
#include <vector>
class Solution {
public:
//一个简单的思路
vector<vector<int> > rotateMatrix(vector<vector<int> >& mat, int n) {
// write code here
vector<vector<int>> ans(n, vector<int>(n));
vector<int> temp(n*n);
//类似于打点的思路
int k = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
temp[k] = mat[i][j];
k++;
}
}
k = 0;
for(int j = n-1; j >= 0; j-- ){
for(int i = 0; i < n; i++){
ans[i][j] = temp[k++];
}
}
return ans;
}
};
查看9道真题和解析
