二维数组螺旋打印
def rotate(matrix):
m,k,count=len(maxtrix),0,1
while True:
for i in range(count-1,m-count):
print(matrix[count-1][i])
k+=1
for i in range(count-1,m-count):
print(matrix[i][-count])
k+=1
for i in range(count-1,m-count):
print(matrix[-count][-i-1])
k+=1
for i in range(count-1,m-count):
print(matrix[-i-1][count-1])
k+=1
count+=1
if k==m*m:
return
elif k+1==m*m:
print(matrix[m/2+1][m/2+1])
return
matrix =[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
]
rotate(matrix)
网友评论