[LeetCode 题解] Spiral Matrix
前言
【LeetCode 题解】系列传送门:
http://www.cnblogs.com/double-win/category/573499.html
题目链接
题目描述
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的更多相关文章
- 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 ...
- [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 ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 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 ...
- [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 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- IE6 PNG不透明问题 (只解决img标签的图片)
<script type='text/javascript' src="/script/ie6.pngfix.js"></script> 会让一些posit ...
- SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门
SpringMVC在使用过程中,大多是使用注解,对它的实现接口之类的关系理解变得模糊, 通过对XML配置的理解,可以理清各个类的关系,譬如控制器类要实现Controller接口. 接触SpringMV ...
- Resume (Curriculum Vitae)
The resume (Curriculum Vitae) is a selling tool outlining your skills and experience so an employer ...
- X_PU
通俗易懂告诉你CPU/GPU/TPU/NPU...XPU都是些什么鬼?[附把妹秘籍] 2017-10-27 19:54移动芯片/谷歌 作者:iot101君 物联网智库 原创 转载请注明来源和出处 现在 ...
- python之多并发socket
先看socket多并发的服务端的代码,这里是用多线程实现的多并发socketserver import socketserver # socketserver有四个基本的类,后两个不常用,这4个类处理 ...
- springMVC将处理的后的数据通过post方法传给页面时,可能会出现乱码问题,下面提出解决post乱码问题的方法
在web.xml中加入: <!-- 解决post乱码问题 --> <filter> <filter-name>CharacterEncodingFilter< ...
- sql标量值函数,将汉字转化为拼音,无音标
USE [db_Test]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO create function [dbo].[fn_GetPinyin]( ...
- Window 编码 UTF-8 BOM 说明
UTF-8 不需要 BOM,尽管 Unicode 标准允许在 UTF-8 中使用 BOM.所以不含 BOM 的 UTF-8 才是标准形式,在 UTF-8 文件中放置 BOM 主要是微软的习惯(顺便提一 ...
- jmeter写好的脚本检查无误之后就是无法执行成功
今天,用jmeter写好的脚本,检查了好几遍,没有任何错误,但是执行的时候命令发送总是失败,没有cookie,请教高手,才得以解决. 重新创建一个HTTP request,把之前写好的都一一拷贝过来, ...
- [BAT]批处理自动修改区域和语言选项
open a cmd window and type reg query "HKCU\Control Panel\International" which will show yo ...