public class A066_rotateMatrix {
public static int[][] rotateMatrix(int[][] mat, int n) {
int length = mat.length;
//矩阵转置,显示对角线交换
for (int i = 0; i < length; i++) {
for (int j = 0; j < i; j++) {
//交换上三角与下三角对应的元素
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
//然后是每行翻转
for (int i = 0; i < length; i++) {
for (int j = 0; j < length / 2; j++) {
int temp = mat[i][j];
mat[i][j] = mat[i][length - j - 1];
mat[i][length - j - 1] = temp;
}
}
return mat;
}
}
矩阵顺时针反转
最新推荐文章于 2025-11-24 13:22:53 发布
本文介绍了一段Java代码,详细解析了如何通过矩阵转置和特定的元素交换操作,实现矩阵的旋转,包括上三角与下三角元素的互换以及每行的翻转。适合理解矩阵操作和算法实现。
1529

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



