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

顺时针旋转矩阵

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

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param mat int整型二维数组 
     * @param n int整型 
     * @return int整型二维数组
     */
    public int[][] rotateMatrix (int[][] mat, int n) {
        // write code here
        int[][] res = new int[n][n];
        int index = n;
        for(int i = 0; i < mat.length; i++) {
            int[] ch = mat[i];
            res[i] = new int[n];
            for(int j = 0; j < ch.length; j++) {
                res[i][j] = mat[index-1][i];
                index --;
            }
            index = n;
        }

        return res;
    }
}

// [
//     [1,2,3],
//     [4,5,6],
//     [7,8,9]
// ]

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务