-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-12-2024-Rotate by 90 degree.cpp
58 lines (49 loc) · 1.16 KB
/
21-12-2024-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
58
//{ 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
vector<vector<int>>temp = mat;
int k = 0 ,l=0;
for(int i = mat[0].size()-1 ;i>=0; i--){
for(int j = 0; j<mat.size();j++){
temp[k][l++] = mat[j][i];
}
k++;
l = 0;
}
mat = temp;
return ;
}
};
//{ 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