54. Spiral Matrix
題目:
https://leetcode.com/problems/spiral-matrix/
難度:
Medium
參考別人的代碼,一開始覺得很有遞歸性怎憋,根據(jù)奇偶不同來寫烈菌,遞歸太難寫。
然后想到了loop惑芭,再想,可能有更優(yōu)trick,事實證明并沒有贫橙。
用四個變量來控制邊界,然后因為方向總是:→↓←↑ 左右下上
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == [] : return []
res = []
maxUp = maxLeft = 0
maxDown = len(matrix) - 1
maxRight = len(matrix[0]) - 1
direction = 0 # 0 go right, 1 go down, 2 go left, 3 up
while True:
if direction == 0: #go right
for i in range(maxLeft, maxRight+1):
res.append(matrix[maxUp][i])
maxUp += 1
elif direction == 1: # go down
for i in range(maxUp, maxDown+1):
res.append(matrix[i][maxRight])
maxRight -= 1
elif direction == 2: # go left
for i in reversed(range(maxLeft, maxRight+1)):
res.append(matrix[maxDown][i])
maxDown -= 1
else: #go up
for i in reversed(range(maxUp, maxDown+1)):
res.append(matrix[i][maxLeft])
maxLeft +=1
if maxUp > maxDown or maxLeft > maxRight:
return res
direction = (direction + 1 ) % 4