Skip to content

Latest commit

 

History

History
78 lines (63 loc) · 1.92 KB

54-spiral-matrix.md

File metadata and controls

78 lines (63 loc) · 1.92 KB

54. Spiral Matrix - 螺旋矩阵

给定一个包含 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]

题目标签:Array

题目链接:LeetCode / LeetCode中国

题解

数组相关的问题要注意下标越界问题。

Language Runtime Memory
python3 44 ms N/A
class Solution:
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if not matrix or not matrix[0]:
            return []
        res = []
        rows = matrix
        cols = list(zip(*matrix))
        m, n = len(rows), len(cols)
        i = 0
        while True:
            i += 1
            if 0 <= i-1 < m:
                res.extend(rows[i-1][i-1:n-i+1])
                if len(res) == m * n:
                    break
            if 0 <= n-i < n:
                res.extend(cols[n-i][i:m-i+1])
                if len(res) == m * n:
                    break
            if 0 <= m-i < m:
                res.extend(rows[m-i][i-1:n-i][::-1])
                if len(res) == m * n:
                    break
            if 0 <= i-1 < n:
                res.extend(cols[i-1][i:m-i][::-1])
                if len(res) == m * n:
                    break
        return res