题目
给你一个 m
行 n
列的矩阵matrix
,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
示例 1:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5]
示例 2:
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 输出:[1,2,3,4,8,12,11,10,9,5,6,7]
解题
方法一:按顺序遍历
class Solution(object): def spiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ if not matrix or not matrix[0]: return [] M, N = len(matrix), len(matrix[0]) left, right, up, down = 0, N - 1, 0, M - 1 res = [] x, y = 0, 0 dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] cur_d = 0 while len(res) != M * N: res.append(matrix[x][y]) if cur_d == 0 and y == right: cur_d += 1 up += 1 elif cur_d == 1 and x == down: cur_d += 1 right -= 1 elif cur_d == 2 and y == left: cur_d += 1 down -= 1 elif cur_d == 3 and x == up: cur_d += 1 left += 1 cur_d %= 4 x += dirs[cur_d][0] y += dirs[cur_d][1] return res