【leetcode】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.从下到上,结束后第一列已经完成,故标记列的自增
这里也可以看出,会有重复的计算的,但是重复小于一圈,故可以直接采用截断的方式,当然在循环中加入判断亦可以达到这个目的。
class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
if len(matrix) == 0:
return []
r, c = len(matrix), len(matrix[0])
if c == 0:
return []
roundr = 0
roundc = 0
res = []
while roundr < r and roundc < c:
for i in range(roundc, c):
res.append(matrix[roundr][i])
roundr += 1
for i in range(roundr, r):
res.append(matrix[i][c-1])
c -= 1
for i in range(c-1,roundc-1,-1):
res.append(matrix[r-1][i])
r -= 1
for i in range(r-1,roundr-1,-1):
res.append(matrix[i][roundc])
roundc += 1
return res
#return res[0:len(matrix)*len(matrix[0])];
s = Solution()
print s.spiralOrder([
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
])
print s.spiralOrder([[7],[9],[6]])
print s.spiralOrder([[7,9,6]])
【leetcode】Spiral Matrix的更多相关文章
- 【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 ...
- 【leetcode】 Spiral Matrix
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【数组】Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【数组】Spiral Matrix
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
随机推荐
- 在php中验证复选框
PHP接收多个同名复选框信息不像ASP那样自动转换成为数组,这给使用带来了一定不便.但是还是有解决办法的,就是利用javascript做一下预处理.多个同名复选框在javascript中还是以数组的形 ...
- python word
代码: #coding=utf-8 __author__ = 'zhm' from win32com import client as wc import os import time import ...
- React Native 开发之 (01) 配置开发环境
一 React Native React Native 是由Facebook发布的开源框架,着力于提高多平台开发的开发效率 —— 仅需学习一次,编写任何平台.(Learn once, write an ...
- 树莓派系统介绍:DIetPi
项目主页:http://fuzon.co.uk/phpbb/viewtopic.php?f=8&t=6 当前版本:V34(15年4月16日发布) DietPi是国外一个基于Raspbian的精 ...
- QT读取文本(字符串)最后一行的方法
QString str; QTextStream ts(&str); str = this->toPlainText(); ts.seek(str.lastIndexOf("- ...
- go智能提示(重要)
使用VIM开发go程序时,智能提示是一个大问题. 最终解决方案是使用 YCM,它是使用 gocode 来进行智能提示的.一切配置好之后,你会发现标准库和第三方库都可以智能提示,但自己写的包却不能,猜想 ...
- passive 的事件监听器
很久以前,addEventListener() 的参数约定是这样的: addEventListener(type, listener, useCapture) 后来,最后一个参数,也就是控制监听器是在 ...
- Mongo DB 2.6 需要知道的一些自身限定
在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...
- Java开发规范摘录
对于规范的 JAVA 派生类,尽量用 eclipse工具来生成文件格式,避免用手写的头文件/实现文件. 尽量避免一行的长度超过 200 个字符,因为很多终端和工具不能很好处理之.缩进8格 ,impor ...
- 【荐1】Total Commander 7.57 个人使用设置 及 常用快捷键 备忘
Total Commander 7.57a 下载地址:http://www.baidu.com/s?wd=total commander 7.57 破解版 软件整体预览图:(注意,下面的版本我用的是 ...