classSolution:defspiralOrder(self,matrix: List[List[int]]) -> List[int]:iflen(matrix)==0:return [] n, m =len(matrix),len(matrix[0]) num_total, res = n * m, [] left, right, top, bottom =0, m -1,0, n -1whilelen(res)< num_total:iflen(res)< num_total:for j inrange(left, right +1): res.append(matrix[top][j]) top +=1iflen(res)< num_total:for i inrange(top, bottom +1): res.append(matrix[i][right]) right -=1iflen(res)< num_total:for j inrange(right, left -1, -1): res.append(matrix[bottom][j]) bottom -=1iflen(res)< num_total:for i inrange(bottom, top -1, -1): res.append(matrix[i][left]) left +=1return res