-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0048_Rotate_Image.cpp
More file actions
56 lines (48 loc) · 1.48 KB
/
Copy path0048_Rotate_Image.cpp
File metadata and controls
56 lines (48 loc) · 1.48 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
52
53
54
55
56
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = 0; i < n; i++){
for(int j = i; j < n - 1 - i; j++){
/*swap evey four location
(i, j) -> (j, n-1-i)
| |
(n-1-j, i) <- (n-1-i, n-1-j)*/
int tmp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = tmp;
}
}
}
};
int main(){
Solution solve;
/*
vector<vector<int> > input = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
*/
vector<vector<int> > input = {{5, 1, 9, 11},
{2, 4, 8, 10},
{13, 3, 6, 7},
{15, 14, 12, 16}};
cout << "Input: " << endl;
for(int i = 0; i < input.size(); i++){
for(int j = 0; j < input[i].size(); j++)
cout << input[i][j] << " ";
cout << endl;
}
solve.rotate(input);
cout << "Output: " << endl;
for(int i = 0; i < input.size(); i++){
for(int j = 0; j < input[i].size(); j++)
cout << input[i][j] << " ";
cout << endl;
}
return 0;
}