前言

【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. angluarjs ng-repeat 行号

    参考 https://zhidao.baidu.com/question/1882914672116911828.html $index

  2. hdoj1176 免费馅饼(dp 数塔)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 思路: 这道题不复杂,很明显是个dp题,数据比较大,搜索应该会超时,想到如何初始化,注意细节就差 ...

  3. zoj1004-Anagrams by Stack 【栈 dfs】

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 Anagrams by Stack Time Limit: 2 Seconds ...

  4. Frog and Portal(思维好题)

    Frog and Portal https://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 ...

  5. 全国省市区数据库SQL(有可能不是最新的)

    百度云下载地址:https://pan.baidu.com/s/1lStN7tYpwOtpC-r3G2X2sw

  6. MYSQL错误:You can't specify target table for update in FROM clause

    这句话意思是:不能先select再更新(修改)同一个表. 可以再外嵌套多一层,这个问题只有mysql有,mssql和oracle都没有. # 出错delete from Person where Id ...

  7. UVa 10763 Foreign Exchange(map)

    Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates ...

  8. TZOJ 1242 求出前m大的数(预处理)

    描述 给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=10000)并按从大到小的顺序排列. 输入 ...

  9. TZOJ 5280 搜索引擎(模拟字符串)

    描述 谷歌.百度等搜索引擎已经成为了互连网中不可或缺的一部分.在本题中,你的任务也是设计一个搜索论文的搜索引擎,当然,本题的要求比起实际的需求要少了许多. 本题的输入将首先给出一系列的论文,对于每篇论 ...

  10. [leetcode]228. Summary Ranges区间统计

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...