135 - 54 螺旋矩阵
题目
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
解答
感觉python有很多函数,专门为刷题发明的😂😂
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
while matrix:
res += matrix.pop(0)
matrix = list(map(list, zip(*matrix)))[::-1]
return resRuntime: 20 ms, faster than 100.00% of Python3 online submissions for Spiral Matrix.
Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Spiral Matrix.
关键是zip能把两个函数竖着拼起来,然后逆序一下再输出就行了
方法还是绕着外面一点点算进去。。
https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-by-liao-tian-yi-jian/
Runtime: 48 ms, faster than 88.01% of JavaScript online submissions for Spiral Matrix.
Memory Usage: 33.8 MB, less than 36.36% of JavaScript online submissions for Spiral Matrix.
Runtime: 52 ms, faster than 68.49% of JavaScript online submissions for Spiral Matrix.
Memory Usage: 33.8 MB, less than 63.64% of JavaScript online submissions for Spiral Matrix.
Last updated
Was this helpful?