[leetcode]Spiral Matrix @ Python
原题地址:https://oj.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].
解题思路:来段精巧的代码。
代码:
class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
if matrix == []: return []
up = 0; left = 0
down = len(matrix)-1
right = len(matrix[0])-1
direct = 0 # 0: go right 1: go down 2: go left 3: go up
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]Spiral Matrix @ Python的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [LeetCode]Spiral Matrix 54
54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- LeetCode——Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- zk节点扩充
zk节点扩充,从3个节点扩充为7个节点,需要先安装4个节点,在将4个节点的配置进行修改,然后在修改 原有的3个节点.至此完成对zk的扩充实现,在此做个记录. zk节点的顺序,与对应zk与所在序列保持一 ...
- 发布Web端
1.右键发布 2.配置文件,选择自定义 3.填写配置名称 4.选择本地目录 5.最后发布
- 洛谷P1133 教主的花园
题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...
- pop3_用Java发送图文并茂的HTML邮件
package com.syj; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.i ...
- 群晖NAS的Docker容器使用中国镜像加速
vi /var/packages/Docker/etc/dockerd.json 添加如下内容: { "registry-mirrors": ["https://regi ...
- 《廖雪峰 . Git 教程》学习总结
基本上,Git就是以下面的命令顺序学习的.文中笔记是从廖雪峰老师的 Git教程 中总结出来的,方面查阅命令. 1.基础 git config --global user.name "Your ...
- [Go] Cookie 使用简介
Golang 的 Cookie web 开发免不了要和 cookie 打交道.Go 的 http 库也提供了 cookie 的相关操作. type Cookie struct { Name strin ...
- TCPIP网络协议层对应的RFC文档
原文地址:TCPIP网络协议层对应的RFC文档作者:西木 RFC - Request For Comments 请求注解 TCP/IP层 网络协议 RFC文档 Physical Layer Data ...
- java容器类---概述
1.容器类关系图 虚线框表示接口. 实线框表示实体类. 粗线框表示最经常使用的实体类. 点线的箭头表示实现了这个接口. 实线箭头表示类能够制造箭头所指的那个类的对象. Java集合工具包位于Java. ...
- 使用Axure RP原型设计实践02,自定义部件以及熟悉与部件相关面板
本篇体验在Axure中自定义部件,并熟悉Widget Interations and Notes面板,Widget Properties and Style面板,Widget Manager面板. 在 ...