- 题目描述
题目来源
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void rotate(vector<vector<int>>& matrix)
{
int sr = 0;
int sc = 0;
int er = matrix.size() - 1;
int ec = matrix[0].size() - 1;
while (sr < er)
{
rotateEdge(matrix, sr, sc, er, ec);
sr++;
sc++;
er--;
ec--;
}
}
private:
/*
旋转一条边
(a,b) 表示左上角的点
(c,d) 表示右下角的点
*/
void rotateEdge(vector<vector<int>>& matrix, int a, int b, int c, int d)
{
int temp = 0;
for (int i = 0; i < c - a; i++)
{
temp = matrix[a][b + i];
matrix[a][b + i] = matrix[c - i][b];
matrix[c - i][b] = matrix[c][d - i];
matrix[c][d - i] = matrix[a + i][d];
matrix[a + i][d] = temp;
}
}
};
int main()
{
int n = 0;
cin >> n;
vector<vector<int>> matrix(n, vector<int>(n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
}
Solution s;
s.rotate(matrix);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}