-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution048.java
53 lines (48 loc) · 1.07 KB
/
Solution048.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2018/08/06
*/
public class Solution048 {
/**
* 1. 沿主对角线反转
* 2. 水平反转
*
* @param matrix
*/
public void rotate(int[][] matrix) {
rotateByDui(matrix);
rotateByH(matrix);
}
/**
* 沿主对角线翻转
*
* @param matrix
*/
private void rotateByDui(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
/**
* 水平翻转
*
* @param matrix
*/
private void rotateByH(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = temp;
}
}
}
}