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

顺时针旋转矩阵

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

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param mat int整型vector<vector<>> 
     * @param n int整型 
     * @return int整型vector<vector<>>
     */
    vector<vector<int>> rotate(vector<vector<int>> & matrix){  // 模拟转置
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int>> rotated ;
        for(int i = 0; i <m; i++){ // 从第一列开始由左向右遍历
            vector<int> Row;
            for(int j = n-1; j>=0; j--){ // 从第一行开始由下向上遍历
                Row.push_back(matrix[j][i]);
            }
            rotated.push_back(Row);
        }
        return rotated;
    }
    vector<vector<int> > rotateMatrix(vector<vector<int> >& mat, int n) {
        // write code here
   
        vector<vector<int>> matrix = mat;
        matrix = rotate(matrix);
    
        return matrix;
    }
};

全部评论

相关推荐

04-18 15:58
已编辑
门头沟学院 设计
kaoyu:这一看就不是计算机的,怎么还有个排斥洗碗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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