Source of the topic
https://leetcode.com/problems/spiral-matrix/
Given a matrix of m x n elements (m rows, n columns), return all elements of the Matri X in Spiral Order.
Test instructions Analysis
Input:a Matrix
OUTPUT:A List
Conditions: Serpentine Output
Topic ideas
I was thinking there are some different algorithms, and then found that directly right down the left upper order to do just fine, see a blog of the classification done well, the focus is to look at the border situation, directly see the code.
AC Code (PYTHON)
1 classsolution (object):2 defSpiralorder (self, matrix):3 """4 : Type Matrix:list[list[int]]5 : Rtype:list[int]6 """7 ifMatrix = = []:8 return []9up =0Tenleft =0 Onedown = Len (matrix)-1 Aright = Len (matrix[0])-1 - -Direct =0 the -res = [] - - whileTrue: + ifDirect = =0: - forIinchRange (left, right + 1): + res.append (Matrix[up][i]) Aup + = 1 at ifDirect = = 1: - forIinchRange (up, down + 1): - res.append (Matrix[i][right]) -Right-= 1 - ifDirect = = 2: - forIinchRange (right, left-1, 1): in res.append (Matrix[down][i]) -Down-= 1 to ifDirect = = 3: + forIinchRange (down, up-1, 1): - res.append (Matrix[i][left]) theLeft + = 1 * ifUp > DownorLeft >Right : $ returnResPanax NotoginsengDirect = (direct + 1)% 4 - the
[Leetcode] (python): 054-spiral Matrix