题目来源


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. BZOJ1580 : [Usaco2009 Hol]Cattle Bruisers 杀手游戏

    以贝茜为参照物,则贝茜固定于原点,每个杀手是一个圆心在某条射线上的圆. 解出每个杀手可以射杀贝茜的时间区间,然后扫描线即可,时间复杂度$O(n\log n)$. #include<cstdio& ...

  2. Java IO操作

    转自:http://www.cnblogs.com/jyan/articles/2505791.html Johnny Yan的博客 1 InputStream类型 InputStream的作用是标志 ...

  3. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  4. javascript事件大全4

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  5. Java 的class文件关系

    一个java源文件javac出多个class文件出来!是怎么回事? 1. 你在一个文件里定义了几个类的时候,会出现这种情况,比如public class A {}class B {}class C { ...

  6. empty与isset的一点使用体会

    刚在做表单提交的时候,我想检验一下数据是否存在,并用var_dump函数看一下数据.首先看使用isset()的代码 //登录函数 function login(){ if(!isset($_POST) ...

  7. 绕过杀毒软件抓取windows密码

    使用procdump,由于是微软的东西,带微软签名杀软不会报毒. procdump -accepteula -ma lsass.exe lsass.dmp copy出 lsass.dmp到本机. mi ...

  8. 一种少见的跨目录写webshell方法

    http://hi.baidu.com/kwthqquszlbhkyd/item/480716204cfa33c3a5275afa

  9. 内核Makefile编写

    对于大部分内核模块或设备驱动的开发者和使用者来说,最常接触到的就是各层目录下基于kbuild架构的kbuild Makefile文件.主要部分有:1.目标定义,目标定义就是用来定义哪些内容要做为模块编 ...

  10. Network | TCP congestion control

    拥塞控制算法:1. 加性增.乘性减:2. 慢启动:3. 对超时事件作出反应: 整体过程如下: 慢启动->到达阈值->加性增(窗口+1个MSS), 这个阶段叫拥塞避免(CA)->3个冗 ...