-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrix
54 lines (51 loc) · 1.17 KB
/
RotateMatrix
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
#include <bits/stdc++.h>
void rotateMatrix(vector<vector<int>> &nums, int n, int m)
{
// Write your code here
int x=nums[0][m-1];
if(n>1)
x=nums[1][0];
int y;
int top=0,left=0,right=m-1,bottom=n-1;
while(top<bottom && left<right)
{
for(int i=left;i<=right;i++)
{
y=nums[top][i];
nums[top][i]=x;
x=y;
}
top++;
for(int i=top;i<=bottom;i++)
{
y=nums[i][right];
nums[i][right]=x;
x=y;
}
right--;
if(top<=bottom && right>=left)
{
for(int i=right;i>=left;i--)
{
y=nums[bottom][i];
nums[bottom][i]=x;
x=y;
}
bottom--;
}
if(left<=right && top<=bottom)
{
for(int i=bottom ;i>=top;i--)
{
y=nums[i][left];
nums[i][left]=x;
x=y;
}
left++;
}
if(top<bottom)
{
x=nums[top+1][left];
}
}
}