Rotate Image
给一个2d数组, 先上下反转再镜像翻转.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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; } } |