I'm kind of a lazy person. So, I just checked if a pivot has visited the cell or not.

```
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> mat = vector<vector<int>>(n, vector<int>(n, -1));
if(n == 0) return mat;
int i=0, j=-1, num=0;
while(num < n*n)
{
while(j+1 < n && mat[i][j+1] == -1) mat[i][++j] = ++num;
while(i+1 < n && mat[i+1][j] == -1) mat[++i][j] = ++num;
while(j-1 >= 0 && mat[i][j-1] == -1) mat[i][--j] = ++num;
while(i-1 >= 0 && mat[i-1][j] == -1) mat[--i][j] = ++num;
}
return mat;
}
```