-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (27 loc) · 789 Bytes
/
Copy pathSolution.java
File metadata and controls
28 lines (27 loc) · 789 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
class Solution {
//Time Complexity: O(n^2)
//Space Complexity: O(1)
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int n = matrix.length;
int start = 0, end = n - 1;
//首尾行交换
while (start < end) {
int[] temp = matrix[start];
matrix[start] = matrix[end];
matrix[end] = temp;
start ++;
end --;
}
//以主对角线为轴交换
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
}