螺旋问题:都是一个while大循环套四个小循环(左下右上路径)。
def luoxuanshuzu(m,n):
col=m//n
col= col if m%n==0 else col+1
ans=[['*' for __ in range(col) ] for _ in range(n)]
left=0
right=col-1
top=0
bottom=n-1
count=0
while left<=right and top <=bottom :
for i in range(left,right+1):
count+=1
if count>m:
break
ans[top][i]=count
for j in range(top+1,bottom+1):
count += 1
if count>m:
break
ans[j][right] = count
if left <right and top < bottom:
for k in range(right-1,left,-1):
count += 1
if count > m:
break
ans[bottom][k] = count
for kk in range(bottom,top,-1):
count += 1
if count > m:
break
ans[kk][left] = count
top+=1
left+=1
right-=1
bottom-=1
return ans