-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_matrix_90.cpp
More file actions
51 lines (46 loc) · 1.07 KB
/
rotate_matrix_90.cpp
File metadata and controls
51 lines (46 loc) · 1.07 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// you need to implement this function only
void rotate(vector<vector<int>>& matrix) {
// your code here
int n = matrix.size();
vector<vector<int>> temp(n, vector<int>(n));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
temp[j][n-i-1] = matrix[i][j];
}
}
matrix = temp;
return;
}
int main() {
vector<vector<int>> matrix;
int n;
cin >> n;
// please don't modify the main function
for (int i = 0; i < n; i++) {
vector<int> row;
for (int j = 0; j < n; j++) {
int x;
cin >> x;
row.push_back(x);
}
matrix.push_back(row);
}
rotate(matrix);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n - 1) {
cout << matrix[i][j];
} else {
cout << matrix[i][j] << " ";
}
}
if (i != n - 1) {
cout << endl;
}
}
return 0;
}