-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
42 lines (40 loc) · 1021 Bytes
/
solution.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
class Solution
{
public:
vector<vector<int> > matrix;
void solve(int startPos, int startNum, int n)
{
if(n <= 0)
return ;
else
{
for(int i=startPos;i<startPos+n;i++)
{
matrix[startPos][i] = startNum;
startNum++;
}
for(int i=startPos+1;i<startPos+n;i++)
{
matrix[i][startPos+n-1] = startNum;
startNum++;
}
for(int i=startPos+n -2;i> startPos-1;i--)
{
matrix[startPos+n-1][i] = startNum;
startNum++;
}
for(int i=startPos+n -2;i>startPos;i--)
{
matrix[i][startPos] = startNum;
startNum++;
}
solve(startPos+1,startNum,n-2);
}
}
vector<vector<int> > generateMatrix(int n)
{
matrix.resize(n,vector<int>(n));
solve(0,1,n);
return matrix;
}
};