forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
27 lines (25 loc) · 768 Bytes
/
solution.h
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
#include <vector>
using std::vector;
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int>> vec(n, vector<int>(n,-1));
for (int i=0, j=n; value != n*n; ++i, --j)
spiralFilled(i, j, vec);
return vec;
}
private:
void spiralFilled(int beg, int end, vector<vector<int> >& vec)
{
for (int i=beg, j=beg; true; )
{
if (vec[i][j] == -1) vec[i][j] = ++value;
else break;
if (i == beg && j != end-1) { ++j; continue; }
if (j == end-1 && i != end-1) { ++i; continue; }
if (i == end-1 && j != beg) { --j; continue; }
if (j == beg && i != beg) { --i; continue; }
}
}
int value{0};
};