Created
January 5, 2017 03:42
-
-
Save liondancer/cc9aa652f37feb7efd8a79db01c865fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def spiralOrder(matrix): | |
""" | |
:type matrix: List[List[int]] | |
:rtype: List[int] | |
""" | |
if len(matrix) == 0: | |
return [] | |
if len(matrix) == 1: | |
return matrix[0] | |
if len(matrix[0]) == 1: | |
return [i[0] for i in matrix] | |
res = [] | |
row_s = 0; col_s = 0 | |
row_e = len(matrix) - 1; col_e = len(matrix[0]) - 1 | |
while(row_s <= row_e and col_s <= col_e): | |
# L -> R | |
for i in range(col_s, col_e + 1): | |
res.append(matrix[row_s][i]) | |
row_s += 1 | |
# T -> B | |
for i in range(row_s, row_e + 1): | |
res.append(matrix[i][col_e]) | |
col_e -= 1 | |
# R -> L | |
if row_s <= row_e: | |
for i in range(col_e, col_s - 1, -1): | |
res.append(matrix[row_e][i]) | |
row_e -= 1 | |
# B -> T | |
if col_s <= col_e: | |
for i in range(row_e, row_s - 1, -1): | |
res.append(matrix[i][col_s]) | |
col_s += 1 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment