-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateMatrix.java
More file actions
31 lines (25 loc) · 852 Bytes
/
RotateMatrix.java
File metadata and controls
31 lines (25 loc) · 852 Bytes
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
package leetcode.array;
/**
* @ClassName RotateMatrix
* @Description 旋转矩阵 https://leetcode-cn.com/problems/rotate-matrix-lcci/
* @Author changxuan
* @Date 2020/9/11 下午9:08
**/
public class RotateMatrix {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int layer = 0; layer < n/2; layer++) {
int first = layer;
int last = n - 1 - layer;
for (int i = first; i < last; ++i) {
int offset = i - first;
// 存储上边
int top = matrix[first][i];
matrix[first][i] = matrix[last-offset][first];
matrix[last-offset][first] = matrix[last][last-offset];
matrix[last][last-offset] = matrix[i][last];
matrix[i][last] = top;
}
}
}
}