Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
(螺旋式遍历矩阵)
Example:

1. 方向模拟
事先定义四个方向,按照题中的螺旋式的方向在遍历矩阵,过程中遇到边界时不断的修改方向并修改新的边界。具体实现过程如下:
1 | class Solution: |
2. zip函数
在看别人的简单解法中发现用zip不断地旋转矩阵,每次都获得矩阵的第一行就是螺旋式遍历的结果。具体实现方法如下:
1 | class Solution: |