Blog Domain name: Http://www.xnerv.wang
Original title page: https://oj.leetcode.com/problems/spiral-matrix/
Topic Type:
Difficulty Evaluation: ★
This article address: http://blog.csdn.net/nerv3x3/article/details/41628771
Given a matrix of M x n elements (m rows, n columns), return all elements to the matrix in spiral order.
For example,
Given the following matrix:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
You are 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):
m = len (matrix)
if 0 = = m: return
[]
n = Len (matrix[0])
If 0 = N: return
[]
arr = []
round = (min (m, n) + 1)/2 for
x in range (0, round): For
y in range (x, n-x):
arr.append (Matri X[x][y])
for y in range (x + 1, m-x-1):
arr.append (Matrix[y][n-x-1])
if m-2 * x > 1: ###
for y in range (N-x-1, X-1,-1):
arr.append (Matrix[m-x-1][y])
if n-2 * x > 1: ###
fo R y in range (M-x-2, X,-1): ###
arr.append (matrix[y][x)) return
arr