-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate-image.java
More file actions
30 lines (23 loc) · 765 Bytes
/
Copy pathrotate-image.java
File metadata and controls
30 lines (23 loc) · 765 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
class Solution {
public void rotate(int[][] matrix) {
/* First Transpose the matrix
*/
int n = matrix.length;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
int temp = matrix[j][i];
matrix[j][i] = matrix[i][j];
matrix[i][j] = temp;
}
}
/* Once the transpose is done , flip the matrix horizontally, i.e we reverse each row by row
*/
for(int i=0;i<matrix.length;i++){
for(int j=0;j<n/2;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp;
}
}
}
}