[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 ...
随机推荐
- BZOJ3571 : [Hnoi2014]画框
题目是要求最小乘积最小权匹配, 将一种方案看做一个二维点(x,y),x=a值的和,y=b值的和,所有方案中只有在下凸壳上的点才有可能成为最优解 首先要求出两端的方案l,r两个点 l就是a值的和最小的方 ...
- BZOJ4120 : [Baltic2015]Editor
活跃区的操作序列的优先级单调不上升,所以每次undo的一定是一段区间. 以优先级为权值建立可持久化权值线段树,维护优先级在某区间内的最靠后的位置. #include<cstdio> con ...
- 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法
首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...
- 蒟蒻修养之cf橙名计划
因为太弱,蒟蒻我从来没有上过div1(这就是今年的最后愿望啊啊啊啊啊)已达成................打cf几乎每次都是fst...........所以我的cf成绩图出现了惊人了正弦函数图像.. ...
- 【BZOJ】1070: [SCOI2007]修车(费用流+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1070 好神的题!!!orz 首先我是sb不会拆点..... 首先,每一个技术人员维修车辆都有一个先后 ...
- 开学后,板刷usaco!
我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限 他们刷rank的太可恶了 没有usaco银组金组分类的oj太可恶了 没有usaco翻译的太可恶了 没有usaco数据的 ...
- Hibernate批处理操作优化 (批量插入、更新与删除)
问题描述 我开发的网站加了个新功能:需要在线上处理表数据的批量合并和更新,昨天下午发布上线,执行该功能后,服务器的load突然增高,变化曲线异常,SA教育了我一番,让我尽快处理,将CPU负载降低. 工 ...
- hdu Can you find it
这道题也是道二分的题,主要有几个注意点: 1.两个数组的合并的问题,可以将a数组和b数组合并,这样可以降低时间复杂度. 2.二分查找的left和right的变化问题.之前这里一直wa...一定要是le ...
- javascript中的function
function / 对象 所有的变量和方法名的:以字母,$ _开头其他随便,尽量使用英文字母命名,见名知意注意点:不允许使用关键字定义变量和方法的名称====函数即方法,方法即函数====百度:ja ...
- mysql insert插入新形式,再也不需要拼接多重insert啦
注意一下,不能省略表中的任何字段.包括自增id.而且字段的顺序必须和插入表一致 原理是“表插表” INSERT INTO prod_attr select A.* from ( SELECT AS p ...