classSolution: defgenerateMatrix(self, n: int) -> List[List[int]]: L, R, U, D = 0, n - 1, 0, n - 1 ans = [[0for i inrange(n)] for i inrange(n)] num, cnt = 1, n * n while num <= cnt: for i inrange(L, R + 1): ans[U][i] = num num += 1 U += 1 for i inrange(U, D + 1): ans[i][R] = num num += 1 R -= 1 for i inrange(R, L - 1, -1): ans[D][i] = num num += 1 D -= 1 for i inrange(D, U - 1, -1): ans[i][L] = num num += 1 L += 1 return ans