zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    python中a = a+b与a += b的不同
    python中的全局变量global
    python中星号(*)和双星号(**)的用法
    python循环语句
    python逻辑运算符
    python内置函数 print()
    python 解析迅雷下载链接
    python 正则表达式
    python 读写文件
    python selenium操作cookie
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/10234807.html
Copyright © 2011-2022 走看看