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

顺时针旋转矩阵

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

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param mat int整型vector<vector<>> 
     * @param n int整型 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > rotateMatrix(vector<vector<int> >& mat, int n) {
        // write code here
       for(int i = 0; i < n; i++)
       {
            for(int j = 0; j < i; j++)
            {
                swap(mat[i][j], mat[j][i]);
            }
       }
       for(int i = 0; i < n; i++)
       {
            reverse(mat[i].begin(), mat[i].end());
       }
       return mat;
    }
};

先对角翻转mat[i][j] 与 mat[j][i]

再按列交换

全部评论

相关推荐

不放弃的小鱼干很洒脱:好可爱的离职理由
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务