前言

【LeetCode 题解】系列传送门:

http://www.cnblogs.com/double-win/category/573499.html

题目链接

54. Spiral Matrix

题目描述

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

For example,

Given the following matrix:

[

[ 1, 2, 3 ],

[ 4, 5, 6 ],

[ 7, 8, 9 ]

]

You should return [1,2,3,6,9,8,7,4,5].

题意

按照螺旋的方式返回矩阵中的各个元素。

例如,给定如下矩阵

[

[ 1, 2, 3 ],

[ 4, 5, 6 ],

[ 7, 8, 9 ]

]

其对应的结果为 [1, 2, 3, 6, 9, 8, 7, 4, 5]

思路

根据题意提取关键信息:

(1) m x n elements (m rows, n columns), 没有特意强调 m 和n 非负, 因此在边界判断是需要考虑空数组

(2) 螺旋的方式, 从结果可以看出是按照顺时针方向依次遍历的。

每次都是按照一个方向一直遍历, 直到该方向上所有的数据都访问过, 那么就转换到下一个方向。

从上一个方向开始, 每个点可能的方向最多有4个, 如果四个方向的下一个点都被遍历过, 则说明碰到了终点。

解法

class Solution(object):
def bfs(self, matrix, i, j, last = 0):
"""
:type matrix: List[List[int]]
:type i: int current row index
:type j: int current column index
:rtype: void
"""
if self.visited[i][j] == 0:
self.visited[i][j] = 1
self.res.append(matrix[i][j])
retry = 0
while retry < 4:
if last % 4 == 0:
if j + 1 <= self.n - 1 and self.visited[i][j + 1] == 0:
self.bfs(matrix, i, j + 1, last)
elif last % 4 == 1:
if i + 1 <= self.m - 1 and self.visited[i + 1][j] == 0:
self.bfs(matrix, i + 1, j, last)
elif last % 4 == 2:
if j - 1 >= 0 and self.visited[i][j - 1] == 0:
self.bfs(matrix, i, j - 1, last)
elif last % 4 == 3:
if i - 1 >= 0 and self.visited[i - 1][j] == 0:
self.bfs(matrix, i - 1, j, last)
last += 1
retry += 1 def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
self.visited = []
self.res = []
self.m = len(matrix)
if self.m == 0:
# the matrix is empty
return self.res
self.n = len(matrix[0])
for i in range(self.m):
line = []
for j in range(self.n):
line.append(0)
self.visited.append(line[:])
self.bfs(matrix, 0, 0)
return self.res

声明

作者 Double_Win
出处 http://www.cnblogs.com/double-win/p/7979672.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。 若本文对你有所帮助,您的关注和推荐是我们分享知识的动力!

[LeetCode 题解] Spiral Matrix的更多相关文章

  1. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  3. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  6. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  7. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  8. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  9. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. function方法控制是否隐藏部分内容

    $(document).ready(function() { $('input[type=radio][name=IE]').change(function() { if (this.value == ...

  2. Python3 open() 函数

    Python3 open() 函数  Python3 内置函数 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 ...

  3. 使用HttpModule实现网址重写和HttpHandler实现页面静态化冲突的解决办法

    使用HttpModule实现网址重写和HttpHandler冲突的解决办法功能描述:1. 用HttpModule做了一个重写URL的功能,实现所有访问html的请求要经过httpModule处理,如果 ...

  4. 没有无线路由,如何让手机使用电脑的网络xyytit

    前言: 智能手机已经越来越普遍,但国内的无线网络的步伐还是没有跟上智能机的脚步.纵使3G,4G已经相继推出,但国内的资费价格着实有点不接地气,所以无线wifi无疑是智能机使用最多的.各大软件上.设备商 ...

  5. TZOJ 1545 Hurdles of 110m(01背包dp)

    描述 In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperit ...

  6. [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  7. linux用户和组2

    1.用户相关命令 su 切换用户 root -> 普通用户 不需要密码 普通用户之间 需要密码 普通用户->root 需要密码 newgrp 切换组身份 whoami 查看当前登录名称 i ...

  8. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  9. myschool 相思树

    题目描述 一群妖王排成一排站在苦情巨树下,寻找自己的转世恋人.虽然都是妖王,但按照涂山的规定必须进行标号,标号为1的妖王排在最后面,标号为n的妖王排在最前面.每个妖王只有一个妖力值a[i]表示它们现在 ...

  10. centos7 编译安装mysql

    centos 7 安装mySql   1,准备mySql源码安装 #wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar ...