-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day111.cpp
44 lines (34 loc) · 983 Bytes
/
Day111.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
44
// Spiral Order Matrix II
// https://www.interviewbit.com/problems/spiral-order-matrix-ii/
vector<vector<int> > Solution::generateMatrix(int A) {
int n= A;
vector<vector<int>> v(n,vector<int> (A, 0));
int row_start = 0;
int row_end = n - 1;
int col_start = 0;
int col_end = n - 1;
int sum = 1;
while(sum <= n * n) {
for(int i = col_start; i <= col_end; i++) {
v[row_start][i] = sum;
sum += 1;
}
row_start += 1;
for(int i = row_start; i <= row_end; i++) {
v[i][col_end] = sum;
sum += 1;
}
col_end -= 1;
for(int i = col_end; i >= col_start; i--) {
v[row_end][i] = sum;
sum += 1;
}
row_end -= 1;
for(int i = row_end; i >= row_start; i--) {
v[i][col_start] = sum;
sum += 1;
}
col_start += 1;
}
return v;
}