-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate Matrix
More file actions
57 lines (43 loc) · 1.14 KB
/
Copy pathRotate Matrix
File metadata and controls
57 lines (43 loc) · 1.14 KB
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
#include <bits/stdc++.h>
#include <bits/stdc++.h>
void rotateMatrix(vector<vector<int>> &mat, int n, int m)
{
// int n = mat.size();
// int m = mat[0].size();
int row=n,col=m;
if(n==1||m==1)return;
int ur = 0, lr = n - 1, rc = m - 1, lc = 0;
int k=0;
while ((ur <= lr) and (lc <= rc))
{
for (int i = lc; i <= rc and ((ur <= lr) and (lc <= rc)); i++)
{
// ans.push_back(mat[ur][i]);
swap(mat[k][k],mat[ur][i]);
}
ur++;
for (int i = ur; i <= lr and ((ur <= lr) and (lc <= rc)); i++)
{
// ans.push_back(mat[i][rc]);
swap(mat[k][k],mat[i][rc]);
}
rc--;
for (int i = rc; i >= lc and ((ur <= lr) and (lc <= rc)); i--)
{
// ans.push_back(mat[lr][i]);
swap(mat[k][k],mat[lr][i]);
}
lr--;
for (int i = lr; i >= ur and ((ur <= lr) and (lc <= rc)); i--)
{
// ans.push_back(mat[i][lc]);
swap(mat[k][k],mat[i][lc]);
}
lc++;
k++;
row-=2;
col-=2;
if(row==1 || col==1)break;
}
return ;
}