forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
61 lines (50 loc) · 1.44 KB
/
main.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
/// Source : https://leetcode.com/problems/rotate-image/description/
/// Author : liuyubobobo
/// Time : 2018-09-13
#include <iostream>
#include <vector>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = 0; i <= n / 2; i ++)
for(int j = i; j < n - 1 - i; j ++){
int t = 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] = t;
Solution::print_matrix(matrix);
}
}
static void print_matrix(const vector<vector<int>>& m){
for(int i = 0; i < m.size(); i ++){
for(int j = 0; j < m[i].size(); j ++)
cout << m[i][j] << " ";
cout << endl;
}
cout << endl;
}
};
int main() {
vector<vector<int>> matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Solution().rotate(matrix1);
Solution::print_matrix(matrix1);
vector<vector<int>> matrix2 = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
Solution().rotate(matrix2);
Solution::print_matrix(matrix2);
return 0;
}