classSolution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n, vector<int>(n)); int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; vector<vector<bool>> vis(n, vector<bool>(n)); for(int i = 1, x = 0, y = 0, d = 0; i <= n * n; i++){ res[x][y] = i; vis[x][y] = true; int a = x + dx[d], b = y + dy[d]; if(a < 0 || a >= n || b < 0 || b >= n || vis[a][b]){ d = (d + 1) % 4; a = x + dx[d], b = y + dy[d]; } x = a, y = b; } return res; } };