Rotate Image
Rotate a matrix clockwise, 90 degree.
do it in-place.
how to do it in place?
remember, do it in place doesn’t mean that we don’t need extra space. in fact, we do.
so if we takes an element and put it down in its new place, the original element here will be covered. so in order to do it in place, we need to use swap.
which means every step needs to be down in a symmetric way.
in this case, firstly, we need to flip it by it orthodox and secondly we need to flip it by its y-axis
class Solution {
public void rotate(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = i; j < matrix[0].length; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[0].length - j - 1];
matrix[i][matrix[0].length - j - 1] = temp;
}
}
}
}

浙公网安备 33010602011771号