-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007 Rotate Matrix.cpp
43 lines (42 loc) · 980 Bytes
/
007 Rotate Matrix.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
#include <bits/stdc++.h>
void rotateMatrix(vector<vector<int>> &mat, int n, int m)
{
int i, j, left, right, top, bottom, k, temp;
left = 0;
top = 0;
right = m - 1;
bottom = n - 1;
while (top < bottom && left < right)
{
temp = mat[top][left];
for (i = left + 1; i <= right; i++)
{
k = mat[top][i];
mat[top][i] = temp;
temp = k;
}
top++;
for (i = top; i <= bottom; i++)
{
k = mat[i][right];
mat[i][right] = temp;
temp = k;
}
right--;
for (i = right; i >= left; i--)
{
k = mat[bottom][i];
mat[bottom][i] = temp;
temp = k;
}
bottom--;
for (i = bottom; i >= top; i--)
{
k = mat[i][left];
mat[i][left] = temp;
temp = k;
}
left++;
mat[top - 1][left - 1] = temp;
}
}