## [面試題29. 順時針打印矩陣](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
>Easy
#### 思路
* 按著題目理解的方式進行遍歷
* 一圈分成4個步驟,代碼使用 `%4` 來區分步驟
* 一圈結束后,要向內圈縮進一格
* 遍歷次數為m * n
#### 代碼
python3
```
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix) == 0:
return []
r = []
m = len(matrix)
n = len(matrix[0])
count = 0
i = 0
j = 0
for k in range(m * n):
#print((i,j))
r.append(matrix[i][j])
if count % 4 == 0:
if j < (n - 1 - int(count / 4)):
j +=1
else:
i+=1
count += 1
elif count % 4 == 1:
if i < (m - 1 - int(count / 4)):
i +=1
else:
j-=1
count +=1
elif count % 4 == 2:
if j > int(count / 4):
j-=1
else:
i-=1
count +=1
else:
if i> int(count/4) + 1:
i-=1
else:
j+=1
count +=1
return r
```
- 目錄
- excel-sheet-column-number
- divide-two-integers
- house-robber
- fraction-to-recurring-decimal
- profile
- kids-with-the-greatest-number-of-candies
- qiu-12n-lcof
- new-21-game
- product-of-array-except-self
- minimum-depth-of-binary-tree
- univalued-binary-tree
- shun-shi-zhen-da-yin-ju-zhen-lcof
- permutations
- satisfiability-of-equality-equations
- word-ladder-ii
- ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
- palindrome-number
- network-delay-time
- daily-temperatures
- longest-common-prefix
- sum-of-mutated-array-closest-to-target
- 周賽專題
- make-two-arrays-equal-by-reversing-sub-arrays
- check-if-a-string-contains-all-binary-codes-of-size-k
- course-schedule-iv
- cherry-pickup-ii
- maximum-product-of-two-elements-in-an-array
- maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
- reorder-routes-to-make-all-paths-lead-to-the-city-zero
- probability-of-a-two-boxes-having-the-same-number-of-distinct-balls
- shuffle-the-array
- the-k-strongest-values-in-an-array
- design-browser-history
- paint-house-iii
- final-prices-with-a-special-discount-in-a-shop