Leetcode Day11 3
劍指 Offer 29. 順時(shí)針打印矩陣
輸入一個(gè)矩陣,按照從外向里以順時(shí)針的順序依次打印出每一個(gè)數(shù)字。
示例 1:
輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
輸出:[1,2,3,6,9,8,7,4,5]
示例 2:
輸入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
輸出:[1,2,3,4,8,12,11,10,9,5,6,7]
嗯就是主要是為上下左右設(shè)定四個(gè)方向,遍歷一次后,上下左右的邊界隨之改變。
另外debug了很久終于發(fā)現(xiàn)了問題,python一定要注意縮進(jìn)啊啊啊

class?Solution:
????def?spiralOrder(self,?matrix:?List[List[int]])?->?List[int]:
????????if?not?matrix:return?[]
????????b=len(matrix)-1
????????r=len(matrix[0])-1
????????res=[]
????????l=0
????????t=0
????????while?True:
????????????for?i?in?range(l,r+1):
????????????????res.append(matrix[t][i])
????????????t+=1
????????????if?t>b:break
????????????for?i?in?range(t,b+1):
????????????????res.append(matrix[i][r])
????????????r-=1
????????????if?l>r:break
????????????for?i?in?range(r,l-1,-1):
????????????????res.append(matrix[b][i])
????????????b-=1
????????????if?t>b:break
????????????for?i?in?range(b,t-1,-1):
????????????????res.append(matrix[i][l])
????????????l+=1
????????????if?l>r:break
????????return?res
