题解 | 顺时针旋转矩阵

顺时针旋转矩阵

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

import java.util.*;


public class Solution {
    /**
     * 1 2 3
     * 4 5 6
     * 7 8 9
     * <p>
     * 7 4 1
     * 8 5 2
     * 9 6 3
     * <p>
     * 旋转矩阵,实际是将(i,j)位置的元素放到了新位置(j,n-1-i)
     */
    public int[][] rotateMatrix(int[][] mat, int n) {
        int[][] newMat = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                newMat[j][n - 1 - i] = mat[i][j];
            }
        }
        return newMat;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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