题目描述
- 螺旋矩阵
https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/
https://leetcode-cn.com/problems/spiral-matrix/submissions/ - 螺旋矩阵 II
https://leetcode-cn.com/problems/spiral-matrix-ii/ - 螺旋矩阵 III
https://leetcode-cn.com/problems/spiral-matrix-iii/
思路题解
54. 螺旋矩阵-递归法
设置四个边界,每弄完一个边之后,边界+1 or -1
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
self.ans=[]
def printmatrix(self,matrix,i,j,u,r,d,l):#up right down left
# print(self.ans,u,d,l,r,i,j)
if u>d or l>r:return
if (u==d and l==r):
self.ans.append(matrix[i][j])
return
while j<=r and u<=d and l<=r:#→
if j==r:
self.ans.append(matrix[i][j])
u+=1
i+=1
break
self.ans.append(matrix[i][j])
j+=1
while i<=d and u<=d and l<=r:#↓
if i==d:
self.ans.append(matrix[i][j])
r-=1
j-=1
break
self.ans.append(matrix[i][j])
i+=1
while j>=l and u<=d and l<=r:#←
if j==l:
self.ans.append(matrix[i][j])
d-=1
i-=1
break
self.ans.append(matrix[i][j])
j-=1
while i>=u and u<=d and l<=r:#↑
if i==u:
self.ans.append(matrix[i][j])
l+=1
j+=1
break
self.ans.append(matrix[i][j])
i-=1
printmatrix(self,matrix,i,j,u,r,d,l)
if len(matrix)==0:return self.ans
printmatrix(self,matrix,0,0,0,len(matrix[0])-1,len(matrix)-1,0)
return self.ans
剑指offer:
力扣:
54. 螺旋矩阵-非递归法
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m,n=len(matrix)-1,len(matrix[0])-1
up,down,left,right=0,m,0,n
ans=[]
while up<=down or left<=right:
if up<=down:
ans.extend(matrix[up][left:right+1])
up+=1
if left<=right:
for i in range(up,down+1):
ans.append(matrix[i][right])
right-=1
if up<=down:
for i in range(right,left-1,-1):
ans.append(matrix[down][i])
down-=1
if left<=right:
for i in range(down,up-1,-1):
ans.append(matrix[i][left])
left+=1
return ans
59. 螺旋矩阵 II-非递归法
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
m=n
matrix=[[0 for i in range(m)]for j in range(m)]
up,down,left,right=0,m-1,0,m-1
count=1
while count<=n**2:
if up<=down:
for i in range(left,right+1):
matrix[up][i]=count
count+=1
up+=1
if left<=right:
for i in range(up,down+1):
matrix[i][right]=count
count+=1
right-=1
if up<=down:
for i in range(right,left-1,-1):
matrix[down][i]=count
count+=1
down-=1
if left<=right:
for i in range(down,up-1,-1):
matrix[i][left]=count
count+=1
left+=1
return matrix
885. 螺旋矩阵 III边界扩张,非递归法
class Solution:
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
up,down,left,right=rStart,rStart,cStart,cStart
ans={}
count=0
while count<rows*cols:
if up>=0:
for i in range(left,right+1):
if (up,i) not in ans:
ans[up,i]=1
count+=1
if right<cols-1:right+=1
if right<=cols-1:
for i in range(up,down+1):
if (i,right) not in ans:
ans[i,right]=1
count+=1
if up>0:up-=1
if down<rows-1:down+=1
if down<=rows-1:
for i in range(right,left-1,-1):
if (down,i) not in ans:
ans[down,i]=1
count+=1
if left>0:left-=1
if left>=0:
for i in range(down,up-1,-1):
if (i,left) not in ans:
ans[i,left]=1
count+=1
# print(up,down,left,right,up<=0,down>=rows-1,left<=0,right>=cols-1)
return list(ans)
885. 螺旋矩阵 III-步长找规律法
class Solution:
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
ans = [[rStart, cStart]]
if rows * cols == 1: return ans
# For walk length k = 1, 3, 5 ...
for k in range(1, 2*(rows+cols), 2):
# For direction (dr, dc) = east, south, west, north;
# and walk length dk...
for dr, dc, dk in ((0, 1, k), (1, 0, k), (0, -1, k+1), (-1, 0, k+1)):
# For each of dk units in the current direction ...
for _ in range(dk):
rStart += dr
cStart += dc
# If on the grid ...
if 0 <= rStart < rows and 0 <= cStart < cols:
ans.append([rStart, cStart])
if len(ans) == rows * cols:
return ans