-
Notifications
You must be signed in to change notification settings - Fork 0
/
48_rotate_image.cpp
81 lines (66 loc) · 1.9 KB
/
48_rotate_image.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
78
79
80
81
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
void printMatrix(std::vector<std::vector<int>>& matrix){
for (std::vector<int> row : matrix){
for (int v : row)
std::cout << v << ' ';
std::cout << std::endl;
}
}
// O(n^2)
class Solution {
public:
void rotate(std::vector<std::vector<int>>& matrix) {
int n = matrix.size();
int a, b, c, d;
int *a_p, *b_p, *c_p, *d_p;
int r_circle;
for (int circle = 0; circle < n/2; circle++){
r_circle = n - circle - 1;
for (int i = circle; i < n - circle - 1; i++){
a_p = &matrix.at(circle).at(i);
b_p = &matrix.at(i).at(r_circle);
c_p = &matrix.at(r_circle).at(n - i - 1);
d_p = &matrix.at(n - i - 1).at(circle);
a = *a_p;
b = *b_p; *b_p = a;
c = *c_p; *c_p = b;
d = *d_p; *d_p = c;
*a_p = d;
}
}
}
};
int main(){
std::cout << "Matrix row by row:" << std::endl;
int v;
std::vector<int> row;
std::string line;
std::istringstream ss;
// get first row and check n
std::getline(std::cin, line);
ss.str(line);
while (ss >> v)
row.push_back(v);
int n = row.size();
// get the rest of the rows
std::vector <std::vector<int>> matrix(n);
matrix.at(0) = row;
for (int i = 1; i < n; i++){
row = {};
std::getline(std::cin, line);
ss.clear();
ss.str(line);
while (ss >> v)
row.push_back(v);
if (row.size() > n)
throw std::runtime_error("Should have been " + std::to_string(n) + " values");
else matrix.at(i) = row;
}
Solution().rotate(matrix);
std::cout << "Rotated:" << std::endl;
printMatrix(matrix);
return 0;
}