前言

【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. 1. Spring boot 之热部署

    1. spring boot 热部署 1.1. springloaded springloaded可以实现修改类文件的热部署.下载地址:springloaded 安装单击Run Configurati ...

  2. c语言定义函数指针和typedef简写

    二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...

  3. Android 获取ROOT权限原理解析

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android玩家中常说的“越狱”有一个更深层次的认识. 二. Root的介绍 1.       Root 的目的 可以让 ...

  4. 为什么大神的UI设计那么高级?答案尽在此文…

    对于每个网页设计师而言,在设计过程中总会碰到需要作出设计决策的时候.也许你的公司并没有全职设计师,而需求上则要求设计出全新的UI:又或者你正在制作一个你自己的个人项目,而你希望它比 Bootstrap ...

  5. OSGi 系列(十三)之 Configuration Admin Service

    OSGi 系列(十三)之 Configuration Admin Service OSGi 的 CM 就是 Configuration Admin Service,是用于管理 Bundle 属性.并在 ...

  6. 如何从dvi生成pdf--------亲测有效果.

    用里面第二个命令. http://blog.csdn.net/u014682350/article/details/46482477

  7. Android窗口背景的优化

    视图有背景,每个窗口也是有背景的.每一Activity是一个窗口,每一个Activity都有不同得背景.界面的绘画顺序如下:窗口——跟视图 ——子视图.当我们的跟视图已经覆盖了整个窗口的时候 ,程序还 ...

  8. async 和 await

    win8 app开发中使用async,await可以更方便地进行异步开发. async,await的使用可参考代码:Async Sample: Example from "Asynchron ...

  9. 协议 protocol

    协议声明类需要实现的的方法,为不同的类提供公用方法,一个类可以有多个协议,但只能有一个父类,即单继承.它类似java中的接口. 正式协议(formal protocol)--------------- ...

  10. Angular 通过注入 $location 获取与修改当前页面URL

    //1.获取当前完整的url路径 var absurl = $location.absUrl(); //http://172.16.0.88:8100/#/homePage?id=10&a=1 ...