本文共 1184 字,大约阅读时间需要 3 分钟。
# -*- coding:utf-8 -*-class Solution: # matrix类型为二维列表,需要返回列表 def printMatrix(self, matrix): # write code here res = [] if len(matrix) == 0: return res top = 0 bottom = len(matrix) - 1 left = 0 right = len(matrix[0]) - 1 while True: for i in range(left, right + 1): res.append(matrix[top][i]) top = top + 1 if top > bottom: break; for i in range(top, bottom +1): res.append(matrix[i][right]) right = right - 1 if right < left: break; for i in range(right, left-1, -1): res.append(matrix[bottom][i]) bottom = bottom - 1 if bottom < top: break; for i in range(bottom, top-1, -1): res.append(matrix[i][left]) left = left + 1 if left > right: break; return res
**
**
class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: res = [] while matrix: res += matrix.pop(0) matrix = list(zip(*matrix))[::-1] return res
转载地址:http://cysl.baihongyu.com/