forked from mdabarik/blind-75-leetcode-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path48--rotate-image.java
31 lines (29 loc) · 1004 Bytes
/
48--rotate-image.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
class Solution {
public void rotate(int[][] matrix) {
int left = 0, right = matrix.length - 1;
while (left < right) {
for (int i = 0; i < right - left; i++) {
int top = left, bottom = right;
// save topLeft value
int topLeft = matrix[top][left + i];
// move bottomLeft to topLeft
matrix[top][left + i] = matrix[bottom - i][left];
// move bottomRight to bottomLeft
matrix[bottom - i][left] = matrix[bottom][right - i];
// move topRight to bottomRight
matrix[bottom][right - i] = matrix[top + i][right];
// move topLeft to topRight
matrix[top + i][right] = topLeft;
}
left++;
right--;
}
}
} // TC: O(M), SC: O(1)
/*
L R
[[5, 1, 9, 11] T
[2, 4, 8, 10]
[13, 3, 6, 7 ]
[15, 14, 12, 16]] B
*/