6/19/2017 - 9:39 AM

54. Spiral Matrix

1. Spiral Matrix
``````class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []:
return []
elements = []
m = len(matrix)
n = len(matrix[0])
row = 0
col = -1
while True:
for i in range(n):
col += 1
elements.append(matrix[row][col])
m -= 1
if m == 0:
break
#####################################
for i in range(m):
row += 1
elements.append(matrix[row][col])
n -= 1
if n == 0:
break
####################################
for i in range(n):
col -= 1
elements.append(matrix[row][col])
m -= 1
if m == 0:
break
####################################
for i in range(m):
row -= 1
elements.append(matrix[row][col])
n -= 1
if n == 0:
break
return elements``````

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.

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]`.