class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return;
}
int row = matrix.length;
int col = matrix[0].length;
//翻折90度 先对折 在竖折
for(int i=0; i< row; i++){
for(int j=0; j <= i; j++){
rotated(matrix, i, j, j, i);
}
}
for(int i=0; i< row; i++){
for(int j = 0; j < col/2; j++){
rotated(matrix, i, j, i, col-1-j);
}
}
}
public void rotated(int[][]matrix, int i, int j, int m, int n){
int temp = matrix[i][j];
matrix[i][j] = matrix[m][n];
matrix[m][n] = temp;
}
}
如果翻折90度 先对折再竖折 matrix[i][j] = matrix[j][i]
如果翻折180度 先横折再竖折 matrix[i][j] = matrix[i][col-1-j]
如果翻折270度 先对折再横折 matrix[i][j] = matrix[col-1-i][j]
本文详细介绍了一种矩阵旋转算法,该算法通过先对折再进行横向或纵向折叠的方式实现矩阵的90度、180度及270度旋转。文章提供了具体的实现代码,并解释了不同旋转角度下元素交换的具体规则。
6626

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



