You are given annxn2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int n = matrix.length;
for (int i = 0; i < n / 2; i++) { // 只处理上半个矩阵
for (int j = 0; j < matrix[0].length; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - i][j];
matrix[n - 1 - i][j] = tmp;
}
}
for (int i = 0; i < n; i++) { // 只处理右上半个三角
for (int j = i; j < matrix[0].length; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
主要难在要in-place。而in-place难在下标控制。这里用的是一层一层地rotate。然后每一层里一格一格地rotate。只用一个额外变量,所以是inplace。
public void rotate(int[][] m) {
if (m == null || m.length == 0 || m.length == 1) {
return;
}
int n = m.length;
int layers = n / 2;
for (int l = 0; l < layers; l++) {
for (int j = l; j < n - l - 1; j++) {
int temp = m[l][j];
m[l][j] = m[n - 1 - j][l];
m[n - 1 - j][l] = m[n - 1 - l][n - 1 - j];
m[n - 1 - l][n - 1 - j] = m[j][n - 1 - l];
m[j][n - 1 - l] = temp;
}
}
}
public int[][] rotateAnt(int[][] m) {
int n = m.length;
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - 1 - i; j++) {
int temp = m[i][j];
m[i][j] = m[j][n - 1 - i];
m[j][n - 1 - i] = m[n - 1 - i][n - 1 - j];
m[n - 1 - i][n - 1 - j] = m[n - 1 - j][i];
m[n - 1 - j][i] = temp;
}
}
return m;
}