-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotate by 90 degree.cpp
57 lines (47 loc) · 1.1 KB
/
Rotate by 90 degree.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to rotate matrix anticlockwise by 90 degrees.
void rotateby90(vector<vector<int>>& mat) {
// code here
int n = mat.size();
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
swap(mat[i][j],mat[j][i]);
}
}
reverse(mat.begin(),mat.end());
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<vector<int> > matrix(n);
for (int i = 0; i < n; i++) {
matrix[i].assign(n, 0);
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
Solution ob;
ob.rotateby90(matrix);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
cout << matrix[i][j] << " ";
cout << endl;
}
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends