[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)


 1 class Solution(object):
 2     def spiralOrder(self, matrix):
 3         """
 4         :type matrix: List[List[int]]
 5         :rtype: List[int]
 6         """
 7         if matrix == []:
 8             return []
 9         up = 0
10         left = 0
11         down = len(matrix) - 1
12         right = len(matrix[0]) - 1
13
14         direct = 0
15
16         res = []
17
18         while True:
19             if direct == 0:
20                 for i in range(left, right + 1):
21                     res.append(matrix[up][i])
22                 up += 1
23             if direct == 1:
24                 for i in range(up, down + 1):
25                     res.append(matrix[i][right])
26                 right -= 1
27             if direct == 2:
28                 for i in range(right, left - 1, -1):
29                     res.append(matrix[down][i])
30                 down -= 1
31             if direct == 3:
32                 for i in range(down, up -1, -1):
33                     res.append(matrix[i][left])
34                 left += 1
35             if up > down or  left > right:
36                 return res
37             direct = (direct + 1) % 4
38
39                     
时间: 2024-11-08 15:20:34

[LeetCode]题解(python):054-Spiral Matrix的相关文章

Leetcode 细节实现题 Spiral Matrix

Spiral Matrix Total Accepted: 12721 Total Submissions: 62094My Submissions 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 ], [

Leetcode 细节实现题 Spiral Matrix II

Spiral Matrix II Total Accepted: 12773 Total Submissions: 41526My Submissions Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2,

[Leetcode][Python]54: Spiral Matrix

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 54: Spiral Matrixhttps://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 foll

054. Spiral Matrix

题目链接:https://leetcode.com/problems/spiral-matrix/description/ Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

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 example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Spiral Matrix leetcode java

题目: 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]. 题解: 这道题是实现题. 考虑2个初始

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的