-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotate_by_90_degree.cpp
77 lines (65 loc) · 1.52 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Rotate by 90 degree
/*
Given a square mat[][]. The task is to rotate it by 90 degrees in clockwise direction without using any extra space.
Example1:
Input: mat[][] = [[1 2 3], [4 5 6], [7 8 9]]
Output:
7 4 1
8 5 2
9 6 3
Example2:
Input: mat[][] = [1 2], [3 4]
Output:
3 1
4 2
Example3:
Input: mat[][] = [[1]]
Output:
1
Constraints:
1 ≤ mat.size() ≤ 1000
1 <= mat[][] <= 100
Expected Time Complexity: O(n^2)
Expected Space Complexity: O(1)
*/
#include <bits/stdc++.h>
using namespace std;
void transpose(vector<vector<int>> &mat) {
int n = mat.size();
for(int i = 0; i < n; ++i)
for(int j = 0; j < i; ++j)
swap(mat[i][j], mat[j][i]);
}
void reverseRows(vector<vector<int>> &mat) {
int n = mat.size();
for(int i = 0; i < n; ++i)
for(int j = 0; j < n/2; ++j)
swap(mat[i][j], mat[i][n-1-j]);
}
void rotate(vector<vector<int>> &mat) {
transpose(mat);
reverseRows(mat);
}
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].resize(n);
for (int j = 0; j < n; j++)
cin >> matrix[i][j];
}
rotate(matrix);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << "\n";
}
cout << "~"
<< "\n";
}
return 0;
}