48. 旋转图像
没什么算法可言,需要动动脑子,调一下就行了
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
void rotate_single(int i, int j, vector<vector<int>> &matrix) {
int len = matrix.size();
int pre_t = matrix[i][j];
int cur_t;
for (int k = 0; k < 4; k++) {
int t = i;
i = j;
j = len - t - 1;
cur_t = matrix[i][j];
matrix[i][j] = pre_t;
pre_t = cur_t;
}
}
void rotate(vector<vector<int>> &matrix) {
int len = matrix.size();
for (int i = 0; i < len / 2; i++)
for (int j = i; j < len - i - 1; j++)
rotate_single(i, j, matrix);
}
};
int main() {
Solution s;
vector<vector<int>> matrix = {
{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5},
{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5},
};
s.rotate(matrix);
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}