def clockwisePrint(matrix):
res = []
while matrix:
res += matrix.pop(0)
if matrix:
index = 0
for row in matrix:
res.append(row.pop()) # only pop element
if not row: #delete empty row in case [[],[],[]]
matrix.pop(index)
index += 1
if matrix:
res += matrix.pop()[::-1]
if matrix:
index = len(matrix)
for row in matrix[::-1]:
index -= 1
res.append(row.pop(0))
if not row:
matrix.pop(index)
return res
def antiClockwisePrint(matrix):
res = []
while matrix:
index = 0
for row in matrix:
res.append(row.pop(0))
if not row:
matrix.pop(index)
index += 1
if matrix:
res += matrix.pop()
if matrix:
index = len(matrix)
for row in matrix[::-1]:
index -= 1
res.append(row.pop())
if not row:
matrix.pop(index)
if matrix:
res += matrix.pop(0)[::-1]
return res
python顺时针(逆时针)打印列表
猜你喜欢
转载自blog.csdn.net/weixin_42779018/article/details/86674440
今日推荐
周排行