-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathRotate Image
More file actions
50 lines (44 loc) · 1.09 KB
/
Rotate Image
File metadata and controls
50 lines (44 loc) · 1.09 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--------------------------------------------------------------
@Sayak
Rotate the image by 90 degrees (clockwise).
Given input matrix =
[
[ 8, 1, 9,12],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[18,14,12,16]
],
rotate the input matrix in-place such that it becomes:
[
[18,13, 2, 8],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,12]
]
The solution can be achieved int two steps:
1) Transpose the matrix
2) Reverse the matrix rows start<->end , start++, end--
--------------------------------------------------------------
class Solution {
public void rotate(int[][] matrix) {
int n= matrix.length;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
for(int i=0;i<n;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;
}
}
}
}