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

顺时针旋转矩阵

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

https://blog.nowcoder.net/detail/0?qurl=/practice/2e95333fbdd4451395066957e24909cc

这题其实就是找规律,所谓的矩阵顺时针旋转,其实就是将原来的矩阵每列从下到上写到新矩阵的每行从左到右

public class Solution {
    public int[][] rotateMatrix(int[][] mat, int n) {
        if(mat==null||mat[0].length==0) return new int[][]{};
        int rows = n;
        int cols = n;
        int[][] res = new int[cols][rows];
        for(int col = 0;col<cols;col++){
            int w = 0;
            for(int row = rows-1;row>=0;row--){
                //每列从下到上填入新数组的每行从左到右
                res[col][w++] = mat[row][col];
            }
        }
        return res;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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