void support(vector<vector<int>> &res, int row, int col, int len, int &num)
{
if (len <= 0)
return;
int i = row, j = col;
if (len == 1)
{
res[i][j] = num++;
return;
}
while (j<col+len-1)
{
res[i][j++]=num++;
}
while (i<row+len-1)
{
res[i++][j] = num++;
}
while (j > row)
{
res[i][j--] = num++;
}
while (i>col)
{
res[i--][j] = num++;
}
support(res, row + 1, col + 1, len - 2, num);
}
vector<vector<int>> generateMatrix(int n)
{
int num = 1;
vector<vector<int>> res(n,vector<int>(n));
support(res, 0, 0, n, num);
return res;
}
leetcode ----59. Spiral Matrix II
最新推荐文章于 2024-09-07 19:07:25 发布