题目来源


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.


题意分析


Input: a matrix

Output: a list

Conditions: 蛇形输出


题目思路


自己本来在想有没有一些不一样的算法,然后发现直接右下左上顺序做就好了,看到一个博客的分类做得不错,重点是要看边界情况,直接看代码。


AC代码(Python)


 class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []:
return []
up = 0
left = 0
down = len(matrix) - 1
right = len(matrix[0]) - 1 direct = 0 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]题解(python):054-Spiral Matrix的更多相关文章

  1. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  2. LeetCode(59)SPiral Matrix II

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

  3. Java for LeetCode 054 Spiral Matrix

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

  4. 054. Spiral Matrix

    题目链接:https://leetcode.com/problems/spiral-matrix/description/ Given a matrix of m x n elements (m ro ...

  5. 054 Spiral Matrix 旋转打印矩阵

    给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...

  6. LeetCode(54)Spiral Matrix

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

  7. LeetCode 题解 Search a 2D Matrix II。巧妙!

    [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...

  8. [LeetCode 题解]: Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...

  10. Java for LeetCode 059 Spiral Matrix II

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

随机推荐

  1. BZOJ1077 : [SCOI2008]天平

    首先通过差分约束系统建图,用Floyed算法求出任意两个砝码差值的上下界. 然后暴力枚举放在右边的砝码C,D,通过与A,B差值的上下界分类讨论统计方案. 时间复杂度$O(N^3)$. #include ...

  2. BZOJ4294 : [PA2015]Fibonacci

    斐波那契数列模$10^m$的循环节为$6\times10^m$,于是从低位到高位dfs即可. #include<cstdio> #include<cstring> #defin ...

  3. 深入理解JVM—性能监控工具

    (转自:http://yhjhappy234.blog.163.com/blog/static/31632832201222691738865/) 我们知道,在JVM编译期和加载器,甚至运行期已经做了 ...

  4. jQuery 跨域访问问题解决方法(转)

    转自:http://www.jb51.net/article/21213.htm 浏览器端跨域访问一直是个问题, 多数研发人员对待js的态度都是好了伤疤忘了疼,所以病发的时候,时不时地都要疼上一疼.记 ...

  5. 使用distinct出现的一个问题

    如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中. 错误的写法:select distinct top 100  userphone  from m ...

  6. jsoup httpclient 爬取网页并下载google图标

    jsoup下载地址 http://www.jsoup.org httpclient下载地址 http://hc.apache.org/downloads.cgi 其他jar包见附件 Crawler p ...

  7. Odoo 外协加工产品的实现

    需求,当前有产品A需要讲原料B.C送到加工厂P,由加工厂P加工完成后送回公司,然后再销售给客户. 1.首先,设置产品A的BOM由原料B,C组成,产品A的Route设置为Manufacture,产品A的 ...

  8. hdu Proud Merchants

    此题是一个背包的题目,刚开始我并没有作任何的排序工作,所以出来的结果总是错的,仔细想想也确实是不对的,因为q[i]会限制dp[i]的值的变化.虽然我知道要按照某个量进行排序,对原数据进行处理,但是实在 ...

  9. 零宽度正预测先行断言是什么呢,看msdn上的官方解释定义

    最近为了对html文件进行源码处理,需要进行正则查找并替换.于是借着这个机会把正则系统地学一下,虽然以前也用过正则,但每次都是临时学一下混过关的.在学习的过程中还是遇到不少问题的,特别是零宽断言(这里 ...

  10. 修改CSV中的某些值 -- 2

    C:\aaa.csv "IPAddress","FullDomainName","RequestedTargetGroupName" &qu ...