题解 | #顺时针旋转矩阵#

顺时针旋转矩阵

http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc

先进行上下翻转,再进行斜对角线翻转,可实现90度旋转。

class Solution {
public:
    vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) {
        // write code here
        int temp;
        for(int i=0; i<n/2; i++){
            for(int j=0; j<n; j++){
                temp = mat[i][j];
                mat[i][j] = mat[n-i-1][j];
                mat[n-i-1][j] = temp;
            }
        }
        
        for(int i=0; i<n; i++){
            for(int j=0; j<i; j++){
                temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
            }
        }
        return mat;
    }
};
全部评论

相关推荐

tongx_:海投吧同学,面试中能学到更多东西呢,比如拷打项目,要是觉得没准备好就可以从中厂开始呢,但是腾讯都是无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务