输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
示例 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: def spiralOrder(self, matrix:[[int]]) -> [int]: if not matrix: return [] l, r, t, b, res = 0, len(matrix[0]) - 1, 0, len(matrix) - 1, [] while True: for i in range(l, r + 1): res.append(matrix[t][i]) # left to right t += 1 if t > b: break for i in range(t, b + 1): res.append(matrix[i][r]) # top to bottom r -= 1 if l > r: break for i in range(r, l - 1, -1): res.append(matrix[b][i]) # right to left b -= 1 if t > b: break for i in range(b, t - 1, -1): res.append(matrix[i][l]) # bottom to top l += 1 if l > r: break return res
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/13049771.html