描述
You are given an n × n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
You are given an n × n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up: Could you do this in-place?
#include<iostream>
#include<vector>
using namespace std;
const int n = 4;
void RotataImage(int a[][n], int m)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
int temp = a[i][j];
a[i][j] = a[n - j - 1][n - i - 1];
a[n - j - 1][n - i - 1] = temp;
}
}
for (int i = 0; i < m / 2; i++)
{
for (int j = 0; j < n; j++)
{
int temp = a[i][j];
a[i][j] = a[n - i - 1][j];
a[n - i - 1][j] = temp;
}
}
}
int main()
{
int a[n][n] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
RotataImage(a, n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << a[i][j] << " ";
cout << endl;
}
}