题目
题目链接
题解
方法1:两次反转
class Solution {
/**
* 最重目标:newMatrix[i][j] = matrix[j][n - i - 1]
* 分解目标1(上下反转):p1Matrix[i][j] = matrix[n - i - 1][j]
* 分解目标2(主对角线反旋转):p2Matrix[i][j] = matrix[j][i]
*/
public void rotate(int[][] matrix) {
verticalFlip(matrix);
majorDiagonalFlip(matrix);
}
/**
* 垂直反转
*/
private void verticalFlip(int[][] matrix) {
for (int i = 0; i < matrix.length / 2; i++) {
for (int j = 0; j < matrix[i].length; j++) {
int t = matrix[i][j];
matrix[i][j] = matrix[matrix.length - i - 1][j];
matrix[matrix.length - i - 1][j] = t;
}
}
}
/**
* 主对角线反转
*/
private void majorDiagonalFlip(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = i + 1; j < matrix[i].length; j++) {
int t = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = t;
}
}
}
}