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

顺时针旋转矩阵

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

全部评论

相关推荐

ic透明人:少了再加上牛客运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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