54. Spiral Matrix

Posted bernieloveslife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了54. Spiral Matrix相关的知识,希望对你有一定的参考价值。

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

很蠢得用了模拟法。。

class Solution:
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if len(matrix)==0:
            return []
        m,n = len(matrix),len(matrix[0])
        flag = [[True for i in range(n)]for j in range(m)]
        i,j,dir = 0,0,0
        res = [matrix[0][0]]
        flag[0][0] = False
        while True:
            if dir==0: #go right
                if j<n-1 and flag[i][j+1]:
                    res.append(matrix[i][j+1])
                    j += 1
                    flag[i][j] = False
                else:
                    dir = 1
                    if i==m-1 or flag[i+1][j] is False:
                        break
                    else:
                        res.append(matrix[i+1][j])
                        i += 1
                        flag[i][j] = False
                continue
            if dir==1: #go down
                if i<m-1 and flag[i+1][j]:
                    res.append(matrix[i+1][j])
                    i += 1
                    flag[i][j] = False
                else:
                    dir = 2
                    if j==0 or flag[i][j-1] is False:
                        break
                    else:
                        res.append(matrix[i][j-1])
                        j -= 1
                        flag[i][j] = False
                continue
            if dir==2: #go left
                if j>0 and flag[i][j-1]:
                    res.append(matrix[i][j-1])
                    j -= 1
                    flag[i][j] = False
                else:
                    dir = 3
                    if i==0 or flag[i-1][j] is False:
                        break
                    else:
                        res.append(matrix[i-1][j])
                        i -= 1
                        flag[i][j] = False
                continue
            if dir==3: #go up
                if i>0 and flag[i-1][j]:
                    res.append(matrix[i-1][j])
                    i -= 1
                    flag[i][j] = False
                else:
                    dir = 0
                    if j==n-1 or flag[i][j+1] is False:
                        break
                    else:
                        res.append(matrix[i][j+1])
                        j += 1
                        flag[i][j] = False
                continue
        return res

以上是关于54. Spiral Matrix的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 54. Spiral Matrix

54. Spiral Matrix && 59. Spiral Matrix II

#Leetcode# 54. Spiral Matrix

54. Spiral Matrix

54. Spiral Matrix

LeetCode 54. Spiral Matrix