-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0091_1.cpp
More file actions
36 lines (36 loc) · 996 Bytes
/
DAY0091_1.cpp
File metadata and controls
36 lines (36 loc) · 996 Bytes
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
// 59. Spiral Matrix II
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>>answer(n,vector<int>(n,0));
int left=0,right=n-1,top=0,bottom=n-1;
int i=0;
while(left<=right&&top<=bottom){
for(int col=left;col<=right;col++){
answer[top][col]=i+1;
i++;
}
top++;
for(int row=top;row<=bottom;row++){
answer[row][right]=i+1;
i++;
}
right--;
if(top<=bottom){
for(int col=right;col>=left;col--){
answer[bottom][col]=i+1;
i++;
}
bottom--;
}
if(left<=right){
for(int row=bottom;row>=top;row--){
answer[row][left]=i+1;
i++;
}
left++;
}
}
return answer;
}
};