[Leetcode][Python]54: Spiral Matrix
# -*- 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的更多相关文章
- 【LeetCode】54. Spiral Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 【一天一道LeetCode】#54. Spiral Matrix
一天一道LeetCode系列 (一)题目 Given a matrix of m x n elements (m rows, n columns), return all elements of th ...
- 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 ...
- 【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 ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 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 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
随机推荐
- SQL Server索引语法 <第四篇>
从CREATE开始 通过显式的CREATE INDEX命令 在创建约束时作为隐含的对象 随约束创建的隐含索引 当向表中添加如下两种约束之一时,就会创建隐含索引. 主键约束(聚集索引) 唯一约束(唯一索 ...
- HttpWebRequest.Method 属性
public static void GetHead(string url) { var http = (HttpWebRequest)WebRequest.Create(url); http.Met ...
- C#/vbscript/JS如何加密保护HTML/javascript源代码
原文地址:http://www.coding123.net/article/20121008/encrypt-javascript-by-charp-vbscript.aspx 本文通过将源代码进行u ...
- Lake Counting (POJ No.2386)
有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼? *** *W* *** /** *进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 ...
- Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏
Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...
- GDB+GDBServer调试Linux应用程序
参考:http://blog.csdn.net/shanghaiqianlun/article/details/7820401 一.gdb+gdbserver总体介绍 远程调试环境由宿主机GDB和目标 ...
- Unity 梯子生成算法
Unity之生成梯子算法的实现. 1.通过预制物体动态生成角度可设置的梯子形状. 1.1 主要涉及到的数学知识点,角度与弧度的转化. 弧度=角度乘以π后再除以180 角度=弧度除以π再乘以180 1. ...
- Unity 3D 连接Mysql数据库
要想使用Unity直接连接数据库需要以下几个动态库
- centos 6 编译安装httpd-2.4
centos6 yum安装的apr版本已经不适用httpd-2.4版本了.所以,需要源码编译apr以及apr-util 1. 下载源码: cd /usr/local/src/ wget http:// ...
- 将json格式日期(毫秒数)转成日常日期格式和日常格式时间对比
第一:是把生成的Json格式的时间转换,注意要看清楚时间的格式 function (cellval) { var date = new Date(parseInt(cellval.replace(&q ...