原题地址:https://oj.leetcode.com/problems/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].

解题思路:来段精巧的代码。

代码:

class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
if matrix == []: return []
up = 0; left = 0
down = len(matrix)-1
right = len(matrix[0])-1
direct = 0 # 0: go right 1: go down 2: go left 3: go up
res = []
while True:
if direct == 0:
for i in range(left, right+1):
res.append(matrix[up][i])
up += 1
if direct == 1:
for i in range(up, down+1):
res.append(matrix[i][right])
right -= 1
if direct == 2:
for i in range(right, left-1, -1):
res.append(matrix[down][i])
down -= 1
if direct == 3:
for i in range(down, up-1, -1):
res.append(matrix[i][left])
left += 1
if up > down or left > right: return res
direct = (direct+1) % 4

[leetcode]Spiral Matrix @ Python的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  3. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. [LeetCode]Spiral Matrix 54

    54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...

  6. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  7. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  8. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  9. LeetCode——Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

随机推荐

  1. 子类 父类强转 HttpServlet service实现

    相当于 走父类 临时走了一趟 HttpServletRequest ->ServletRequets -> HttpServeltRequest /* */ public void ser ...

  2. react运行阶段

    运行中可以使用的函数componentWillReceiveProps:父组件修改属性触发,可以修改新属性,修改状态.字面意思,组件将要接收属性,这个函数触发的时机就是组件的属性将要发生改变的时候,但 ...

  3. 项目冲刺Second

    Second Sprint 1.各个成员今日完成的任务 蔡振翼:完成管理员界面 谢孟轩:完成用户界面 林凯:完成登录界面 肖志豪:完成主界面 吴文清:创建数据库 刘华强:完成连接数据库,编写查询语句 ...

  4. MyBatis3与Spring3无缝集成-从iBatis平滑过渡

    从2010开始接触iBatis到现在,一直到现在把iBatis作为数据访问层ORM.为了演示一个Web应用,今天又搭了个SpringMVC应用,由于应用比较简单,Spring版本直接用最新版本3.2. ...

  5. BZOJ2612 : [Poi2003]Sums

    设d[i]表示能拼出的x中满足x%a[0]=i的最小的x,其中d[0]=0. 若d[x%a[0]]<=x,则一定可以拼出x,否则一定不可以. 建出带权有向图,点的标号从0到a[0]-1,i号点向 ...

  6. SLAM(一)----学习资料整理

    转自:http://www.cnblogs.com/wenhust/ 书籍: 1.必读经典 Thrun S, Burgard W, Fox D. <Probabilistic robotics& ...

  7. phpexcel导出excel等比例缩放图片

    list($width, $height, $type, $attr) = getimagesize($img_path); if( $width>100 || $height >100 ...

  8. Android四种Activity的加载模式

    建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/archive/2011/04 ...

  9. 关于MongoDB时区问题

    由于MongoDb存储时间按照UTC时间存储的,其官方驱动MongoDB.driver存储时间的时候将本地时间转换为了utc时间,但它有个蛋疼的bug,读取的时候非常蛋疼的是返回的是utc使时间.一个 ...

  10. godaddy 亚太机房 更换 美国机房 全过程(图)

    其它我就不说了,直接干货... 如果要换机房的话,要先支付134元人民币.在哪里支付,怎么支付我就不说了,自己在后台找... 关键的地方来了:当你支付完134元,你回到步骤3会发现没有美国机房选择,呵 ...