【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 ...
随机推荐
- CentOS Linux解决Device eth0 does not seem to be present
在VMware里克隆出来的Centos Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: 解决办法: 首先,打开/etc/udev/rules.d/70 ...
- [转] 64位windows下添加postgreSQL odbc数据源
系统环境:windows7 64位 postgreSQL9.0(64bit) ps:安装postgreSQL时确定安装了odbc驱动. 问题:点击“开始->控制面板->管理工具-& ...
- Spring系列之AOP
一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...
- Bootstrap学习------按钮
Bootstrap为我们提供了按钮组的样式,博主写了几个简单的例子,以后也许用的到. 效果如下 代码如下 <!DOCTYPE html> <html> <head> ...
- [Redis]发布/订阅
摘要 有这样的一个场景,管理员需要发布一条消息,所有的客户端都要受到通知.然后想到了发布订阅模式.使用redis的发布与订阅实现起来更简单一些,说做就做,这里弄个简单的demo,先模拟下. 核心代码 ...
- tyvj1106 登山
背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结…… 不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴 ...
- LYDSY模拟赛day1 String Master
/* 暴力枚举两个后缀,计算最长能匹配多少前缀. 最优策略一定是贪心改掉前 k 个失配的字符. 时间复杂度 O(n3). */ #include<cstdio> ],b[]; int ma ...
- MVVM开发模式简单实例MVVM Demo【续】
本文将接着上篇文章,介绍一下三点:(Universal App) 1.将添加Product集合,绑定到列表 2.给点击ListBox的添加选项改变时的事件(要附加依赖属性,和Button点击事件不同) ...
- HTML5 data-* 属性
HTML5 data-* 属性 jQuery Mobile 依赖 HTML5 data-* 属性来支持各种 UI 元素.过渡和页面结构.不支持它们的浏览器将以静默方式弃用它们.表 2 显示如何使用 d ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...