螺旋矩阵(力扣100) 模拟运动的状态当到四个角的时候需要进行转向并缩小边界class Solution(object): def spiralOrder(self, matrix): :type matrix: List[List[int]] :rtype: List[int] mlen(matrix) nlen(matrix[0]) direction[[0,1],[1,0],[0,-1],[-1,0]] direction_id0 i,j0,0 top,bottom,left,right0,m-1,0,n-1 answer[] for _ in range(m*n): answer.append(matrix[i][j]) next_iidirection[direction_id][0] next_jjdirection[direction_id][1] if not (topnext_ibottom and leftnext_jright): if direction_id0: top1 elif direction_id1: right-1 elif direction_id2: bottom-1 elif direction_id3: left1 direction_id(direction_id1)%4 next_iidirection[direction_id][0] next_jjdirection[direction_id][1] inext_i jnext_j return answer先加入当前的元素并按照预定的方向前进如果超出了边界则说明此时到达了边界处刚走完的这一列或者行就不需要了边界需要缩小并按照修改后的方向重新走。