-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0048.rotate_image.cpp
More file actions
45 lines (38 loc) · 938 Bytes
/
0048.rotate_image.cpp
File metadata and controls
45 lines (38 loc) · 938 Bytes
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
#include <iostream>
#include <vector>
#include "leetcode.h"
using std::vector;
// 先以主对角线翻转(即做转置
// 然后左右镜像
void rotate(vector<vector<int>>& matrix)
{
int n = matrix.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
std::swap(matrix[i][j], matrix[j][i]);
}
}
int m = n >> 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
std::swap(matrix[i][j], matrix[i][n-1-j]);
}
}
}
int main () {
#ifdef LOCAL
freopen("0048.in", "r", stdin);
#endif
int n = 0;
while (std::cin >> n) {
vector<vector<int>> matrix(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
std::cin >> matrix[i][j];
}
}
rotate(matrix);
std::cout << matrix << std::endl;
}
return 0;
}