# -*- 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)

最新文章

  1. IOS NSTimer和CADisplayLink的用法
  2. Android fragment之间消息传递
  3. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
  4. DATEDIFF()(转)
  5. bzoj 2594: [Wc2006]水管局长数据加强版 动态树
  6. Delphi 总结操作Excel
  7. CodeForces 22C System Administrator
  8. js盒子模型
  9. 教你用.Net来玩微信跳一跳
  10. 一简单的RPC实例(Java)
  11. python验证卡普耶卡(D.R.Kaprekar)6174猜想
  12. 通过mysqlbinlog 恢复数据
  13. hello2代码的简单分析
  14. Java中部分常见语法糖
  15. 1分钟试用PowerShell 5.0新功能PowerShellGet安装Script Browser和Script Analyzer
  16. Apache Storm Installation
  17. Restful framework【第十篇】响应器(渲染器)
  18. [剑指Offer]6-从尾到头打印链表
  19. jQuery补充知识点
  20. VC++ 常见问题及其解决方法

热门文章

  1. C# Cookie编程
  2. jmp指令
  3. VC++6.0出现no compile tool is associated with the extension.解决方法
  4. 2.8 Classes of Restricted Estimators
  5. ubuntu cenots 禁止本地登陆
  6. 用Matlab完成:从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
  7. FlashBack-SCN-TIMESTAMP
  8. android onIntent 是什么东西
  9. hadoop python and Twitter
  10. OC——NSDictionary和NSMutableDictionary