-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path161.rotate-image.java
More file actions
36 lines (32 loc) · 1.05 KB
/
161.rotate-image.java
File metadata and controls
36 lines (32 loc) · 1.05 KB
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
public class Solution {
/*
* @param matrix: a lists of integers
* @return:
*/
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return ;
}
// CW: flip up and down, then mirror along diagonal
// CCW: flip left and right, then mirror along diagonal
// constants
int m = matrix.length;
int n = matrix[0].length;
// Step 1: flip up and down
for (int i = 0; i < m / 2; i++) {
for (int j = 0; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[m - 1 - i][j];
matrix[m - 1 - i][j] = temp;
}
}
// Step 2: mirror along diagonal
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
}