-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_rotate.cpp
59 lines (54 loc) · 1.32 KB
/
matrix_rotate.cpp
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
56
57
58
59
// Link of Problem - https://www.interviewbit.com/problems/rotate-matrix/
#include <bits/stdc++.h>
using namespace std;
void rotate(vector<vector<int>>& A){
int n = A[0].size();
int half = n/2;
for (int i = 0; i < half; i++)
{
if (n%2 == 0)
{
for (int j = i; j < n-i; j++)
{
int temp = A[j][n-1-i];
A[j][n-1-i] = A[i][j];
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = temp;
}
}
else
{
for (int j = i; j < n-i; j++)
{
int temp = A[j][n-1-i];
A[j][n-1-i] = A[i][j];
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = temp;
}
}
}
}
int main()
{
vector<vector<int>> A = {{1,2},{3,4}}; // This has to be assumed
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < A.size(); j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
rotate(A);
cout<<endl;
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < A.size(); j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}