[LeetCode]题解(python):054-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.
题意分析
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的更多相关文章
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 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 ...
- 054. Spiral Matrix
题目链接:https://leetcode.com/problems/spiral-matrix/description/ Given a matrix of m x n elements (m ro ...
- 054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...
- LeetCode(54)Spiral Matrix
题目 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...
- 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 ...
- [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 ...
- leetCode题解之Reshape the Matrix
1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...
- 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 ...
随机推荐
- 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)
http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...
- 【wikioi】1913 数字梯形问题(费用流)
http://wikioi.com/problem/1913/ 如果本题没有询问2和3,那么本题和蚯蚓那题一模一样.http://www.cnblogs.com/iwtwiioi/p/3935039. ...
- C#SortedList排序列表怎么样逆序输出
C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...
- 使用freemarker生成word,步骤详解并奉上源代码
1. 步骤 1. 用word编辑好模板 1. 普通字符串替换为 ${string} 2. 表格循环用标签 <#list userList as user> 姓名:${user.u ...
- System-Defined Device Setup Classes Available to Vendors
https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx
- 用Get-ADComputer取非常用属性的值
由于GE使用的是Windows2003+Powershell2.0, 所以某些命令无法使用,比如想取lastLogon和lastLogonTimestamp这两个属性,在Powershell3.0下可 ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2build198)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build198 编译日期:2014-09-17 ====================================== 二. ...
- 【液晶模块系列基础视频】4.5.X-GUI图形界面库-进度条等函数简介
[液晶模块系列基础视频]4.5.X-GUI图形界面库-进度条等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地址 ...
- 使用PHP获取网站Favicon的方法
使用PHP获取网站Favicon的方法 Jan022014 作者:Jerry Bendy 发布:2014-01-02 23:18 分类:PHP 阅读:4,357 views 20条评论 ...
- ExtJS笔记4 容器与布局(Layouts and Containers)
The layout system is one of the most powerful parts of Ext JS. It handles the sizing and positioning ...