/**
* @author xienl
* @description 顺时针旋转矩阵
* @date 2022/7/1
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(solution.rotateMatrix(arr, 3));
}
/**
* 使用复制数组的方式
* @param mat
* @param n
* @return
*/
public int[][] rotateMatrix(int[][] mat, int n) {
int[][] arr = new int[n][n];
int len = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
arr[i][j] = mat[len - j][i];
}
}
return arr;
}
}
牛客网:NC18 顺时针旋转矩阵
最新推荐文章于 2025-12-01 16:54:01 发布
本文介绍了一种通过复制数组实现矩阵顺时针旋转的方法。该算法接受一个n×n的整数矩阵作为输入,并返回一个新的经过顺时针旋转90度后的矩阵。通过遍历原始矩阵并将其元素按旋转规则放置到新矩阵中完成旋转。
951

被折叠的 条评论
为什么被折叠?



