Skip to content

Commit

Permalink
Merge pull request #185 from michal-dbrnowski/Spiral_Matrix
Browse files Browse the repository at this point in the history
Create Spiral_Matrix.py
  • Loading branch information
noisefilter19 authored Oct 12, 2020
2 parents 868c3bc + d3943ba commit 7b7f5e3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Python/Spiral_Matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
k, l = 0, 0
m = len(matrix)
n = len(matrix[0])
res = []
while (k < m and l < n):
for i in range(l, n):
res.append(matrix[k][i])
k += 1
for i in range(k, m):
res.append(matrix[i][n-1])
n -= 1
if (k < m):
for i in range(n-1, (l-1), -1):
res.append(matrix[m-1][i])
m -= 1
if (l < n):
for i in range(m-1, k-1, -1):
res.append(matrix[i][l])
l += 1
return res



0 comments on commit 7b7f5e3

Please sign in to comment.