AI漫剧制作全流程:从剧本分镜到批量成片的实操指南
2026/9/7 11:16:30
LeetCode 54. 螺旋矩阵,经典边界收缩模拟题。
维护四个边界,按顺时针方向逐层遍历:
top → ┌────────┐ │ 1 2 3 │ → right left ↓ │ 4 5 6 │ ↑ │ 7 8 9 │ └────────┘ bottomtop行,然后top++right列,然后right--bottom行,然后bottom--left列,然后left++边界交叉时结束。
classSolution:defspiralOrder(self,matrix:List[List[int]])->List[int]:ifnotmatrixornotmatrix[0]:return[]m,n=len(matrix),len(matrix[0])top,bottom=0,m-1left,right=0,n-1result=[]whiletop<=bottomandleft<=right:# 1. 从左到右遍历 top 行forcolinrange(left,right+1):result.append(matrix[top][col])top+=1# 2. 从上到下遍历 right 列forrowinrange(top,bottom+1):result.append(matrix[row][right])right-=1# 3. 从右到左遍历 bottom 行(需检查是否还有行)iftop<=bottom:forcolinrange(right,left-1,-1):result.append(matrix[bottom][col])bottom-=1# 4. 从下到上遍历 left 列(需检查是否还有列)ifleft<=right:forrowinrange(bottom,top-1,-1):result.append(matrix[row][left])left+=1returnresultif判断,防止在单行或单列矩阵时重复遍历top <= bottom and left <= right确保不越界