-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59.cpp
More file actions
39 lines (39 loc) · 1.08 KB
/
59.cpp
File metadata and controls
39 lines (39 loc) · 1.08 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
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if(n==0)
return {};
vector<vector<int>> matrix(n,vector<int>(n,0));
int val = 1;
int left =0 ,right = n-1, top = 0, bottom = n-1;
int dirs = 0;
while(left<=right && top<=bottom){
if(dirs==0){
for(int i = left; i<=right; i++,val++){
matrix[top][i] = val;
}
top++;
}
else if(dirs==1){
for(int i=top; i<=bottom; i++,val++){
matrix[i][right] = val;
}
right--;
}
else if(dirs==2){
for(int i = right; i>=left; i--,val++){
matrix[bottom][i] = val;
}
bottom--;
}
else{
for(int i=bottom; i>=top; i--,val++){
matrix[i][left] = val;
}
left++;
}
dirs = (dirs+1)%4;
}
return matrix;
}
};