-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateMatrix.cpp
More file actions
55 lines (40 loc) · 1.11 KB
/
RotateMatrix.cpp
File metadata and controls
55 lines (40 loc) · 1.11 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
51
52
53
54
55
/*
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
You need to do this in place.
Note that if you end up using an additional array, you will only receive partial score.
Example:
If the array is
[
[1, 2],
[3, 4]
]
Then the rotated array becomes:
[
[3, 1],
[4, 2]
]
LINK: https://www.interviewbit.com/problems/rotate-matrix/
*/
void Solution::rotate(vector<vector<int> > &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int n = A.size();
int l = 0, h = n-1;
while(l<h){
for(int k=l; k<h;k++){
int a = A[l][k];
int b = A[k][h];
int c = A[h][h-k+l];
int d = A[h-k+l][l];
A[l][k] = d;
A[k][h] = a;
A[h][h-k+l] = b;
A[h-k+l][l] = c;
}
l++;
h--;
}
}