Rotate Image
给一个2d数组, 先上下反转再镜像翻转.
public class Solution {
public void rotate(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
for(int i = 0 ; i < rows / 2; i++) {
for(int j = 0; j < cols; j++) {
swap(matrix, i, j, rows-i-1, j);
}
}
for(int i = 0 ; i < rows; i++) {
for(int j = i; j < cols; j++) {
swap(matrix, i, j, j, i);
}
}
}
public void swap(int[][] m, int x1, int y1, int x2, int y2) {
int tmp = m[x1][y1];
m[x1][y1] = m[x2][y2];
m[x2][y2] = tmp;
}
}