/**
* @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 顺时针旋转矩阵
最新推荐文章于 2024-10-09 14:01:02 发布