mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -131,7 +131,46 @@ public:
|
|||||||
* [59.螺旋矩阵II](https://leetcode-cn.com/problems/spiral-matrix-ii/)
|
* [59.螺旋矩阵II](https://leetcode-cn.com/problems/spiral-matrix-ii/)
|
||||||
* [剑指Offer 29.顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
|
* [剑指Offer 29.顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
|
||||||
|
|
||||||
|
## 其他语言版本
|
||||||
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
|
||||||
|
m, n = len(matrix), len(matrix[0])
|
||||||
|
left, right, up, down = 0, n - 1, 0, m - 1 # 定位四个方向的边界,闭区间
|
||||||
|
res = []
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for i in range(left, right + 1): # 上边,从左到右
|
||||||
|
res.append(matrix[up][i])
|
||||||
|
up += 1 # 上边界下移
|
||||||
|
|
||||||
|
if len(res) >= m * n: # 判断是否已经遍历完
|
||||||
|
break
|
||||||
|
|
||||||
|
for i in range(up, down + 1): # 右边,从上到下
|
||||||
|
res.append(matrix[i][right])
|
||||||
|
right -= 1 # 右边界左移
|
||||||
|
|
||||||
|
if len(res) >= m * n:
|
||||||
|
break
|
||||||
|
|
||||||
|
for i in range(right, left - 1, -1): # 下边,从右到左
|
||||||
|
res.append(matrix[down][i])
|
||||||
|
down -= 1 # 下边界上移
|
||||||
|
|
||||||
|
if len(res) >= m * n:
|
||||||
|
break
|
||||||
|
|
||||||
|
for i in range(down, up - 1, -1): # 左边,从下到上
|
||||||
|
res.append(matrix[i][left])
|
||||||
|
left += 1 # 左边界右移
|
||||||
|
|
||||||
|
if len(res) >= m * n:
|
||||||
|
break
|
||||||
|
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user