# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrix
https://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]. === Comments by Dabay===
一圈一圈的处理,一共处理n/2圈。
注意一些细节,比如圈数,还有同一圈不要重复。
''' class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
if len(matrix) == 0:
return []
if len(matrix) == 1:
return matrix[0]
if len(matrix[0]) == 1:
return [i[0] for i in matrix]
height = len(matrix)
width = len(matrix[0])
res = []
n = 0
while n < (min(height, width)+1)/2:
for i in xrange(n, width-n):
res.append(matrix[n][i])
for i in xrange(n+1, height-n):
res.append(matrix[i][width-1-n])
if n < height-n-1:
for i in reversed(xrange(n, width-n-1)):
res.append(matrix[height-n-1][i])
for i in reversed(xrange(n+1, height-n-1)):
res.append(matrix[i][n])
n += 1
return res def main():
sol = Solution()
matrix = [
[2, 3]
]
print sol.spiralOrder(matrix) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]54: Spiral Matrix的更多相关文章

  1. 【LeetCode】54. Spiral Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  2. 【一天一道LeetCode】#54. Spiral Matrix

    一天一道LeetCode系列 (一)题目 Given a matrix of m x n elements (m rows, n columns), return all elements of th ...

  3. LeetCode OJ 54. 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】54.Spiral Matrix

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

  5. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  6. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  7. 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 ...

  8. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  9. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. zend studio使用入门

    使用zend studio8建立项目 使用PHP开发工具zend studio8进行PHP网站开发,就需要建立(导入)相应的项目,方法如下:右键左侧Workspace,选择New | PHP Proj ...

  2. UESTC_In Galgame We Trust CDOJ 10

    As we all know, there are many interesting (H) games in kennethsnow’s computer. But he sets a passwo ...

  3. cenos 安装 phpredis 扩展

    1. php -m 可以查看 php 所有的已经安装的扩展

  4. java面试题集3

    一.选择题 1.下面程序的运行结果是 int i=0; while(true){ if(i++>10)  break; System.out.println(i); }1-11 2.下面程序的运 ...

  5. LINQ to SQL和Entity Framework对照

    LINQ to SQL和Entity Framework都是一种包括LINQ功能的对象关系映射技术.他们之间的本质差别在于EF对数据库架构和我们查询的类型实行了更好的解耦. 使用EF,我们查询的对象不 ...

  6. Jquery ui datepicker 设置日期范围,如只能隔3天

    最近的后台项目前端使用了jquery ui 日历控件自然就使用了jquery ui 的   datepicker 后台数据比较好大,一般是千万级的和百万级的关联,查询会很慢,所以后加想多加些过滤条件, ...

  7. java学习笔记day05

    1.final关键字:防止被继承的类或覆写的方法修改,变量或方法被final定义后  会在内在中存在 特点:   1)可以修饰类.函数.变量.   2)被final修饰的类不可以被继承.   3)被f ...

  8. C#入门(一):IDE

    设计流程 .NET可视化对象 创建工程的时候,会创建三个文件 Form1.cs Form1.Designer.cs Program.cs 当增加一个控件的时候,会在Form1.Designer.cs增 ...

  9. easyui placeholder 解决方案

    最近,再用easyui的时候,发现easyui的input标签不支持h5的placeholder,为了实现这个效果,提供以下解决方案: 1.给input标签设置placeholder. <td& ...

  10. 浅谈JavaScript DOM编程艺术读后感和一点总结

    最近工作不是很忙就想想想JavaScript的内部组成和一些要点,就是从这本书开始的.对新手来说还好,简单易懂. 简单终结下我重书中学到的一些要点. 下面都是个人学习的要点提取: 1.给自己预留退路, ...