LeetCode 48. 【Java】48. Rotate Image
原题链接
中等
作者:
tt2767
,
2020-04-02 16:32:50
,
所有人可见
,
阅读 563
/**
1. 观察下样例: 先按对角线翻转, 然后按行翻转
*/
class Solution {
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
reverseByDig(matrix);
reverseByrow(matrix);
}
public void reverseByDig(int[][] matrix){
for (int i = 0 ; i < matrix.length; i++)
for (int j = 0; j < i ; j++)
swap(matrix, i, j, j, i);
}
public void reverseByrow(int[][] matrix){
for (int i = 0; i < matrix.length ; i++)
for (int l = 0, r = matrix[0].length - 1; l < r; l ++ ,r --)
swap(matrix, i, l, i, r);
}
public void swap(int[][] matrix , int x1, int y1, int x2, int y2){
int t = matrix[x1][y1];
matrix[x1][y1] = matrix[x2][y2];
matrix[x2][y2] = t;
}
}