题目
给定一个整数矩阵matrix,请按转圈打印的方式打印,如:
1 2 3
4 5 6
7 8 9
打印为:1,2,3,6,9,8,7,4,5
思路
一圈一圈地打印,每圈打印的时候分四个方向打印,圈之间可以递归,也可以循环。
实现1
def cycle_print1(matrix):
def recursive(matrix, row_beg, row_end, col_beg, col_end):
if row_beg > row_end or col_beg > col_end:
return
if row_beg == row_end:
for c in range(col_beg, col_end+1):
result.append(matrix[row_beg][c])
elif col_beg == col_end:
for r in range(row_beg, row_end+1):
result.append(matrix[r][col_beg])
else:
for c in range(col_beg, col_end+1):
result.append(matrix[row_beg][c])
for r in range(row_beg+1, row_end+1):
result.append(matrix[r][col_end])
for c in range(col_end-1, col_beg-1, -1):
result.append(matrix[row_end][c])
for r in range(row_end-1, row_beg, -1):
result.append(matrix[r][col_beg])
recursive(matrix, row_beg+1, row_end-1, col_beg+1, col_end-1)
result = []
if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
return result
recursive(matrix, 0, len(matrix)-1, 0, len(matrix[0])-1)
return result
实现2
def cycle_print2(matrix):
def cycle(matrix, row_beg, row_end, col_beg, col_end):
if row_beg > row_end or col_beg > col_end:
return
if row_beg == row_end:
for c in range(col_beg, col_end+1):
result.append(matrix[row_beg][c])
elif col_beg == col_end:
for r in range(row_beg, row_end+1):
result.append(matrix[r][col_beg])
else:
for c in range(col_beg, col_end+1):
result.append(matrix[row_beg][c])
for r in range(row_beg+1, row_end+1):
result.append(matrix[r][col_end])
for c in range(col_end-1, col_beg-1, -1):
result.append(matrix[row_end][c])
for r in range(row_end-1, row_beg, -1):
result.append(matrix[r][col_beg])
result = []
if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
return
row_beg, row_end = 0, len(matrix)-1
col_beg, col_end = 0, len(matrix[0])-1
while row_beg <= row_end and col_beg <= col_end:
cycle(matrix, row_beg, row_end, col_beg, col_end)
row_beg += 1
row_end -= 1
col_beg += 1
col_end -= 1
return result
测试
def test_cycle_print(row_num, col_num):
mat = []
n = 1
for _ in range(row_num):
row = [n+i for i in range(col_num)]
n += col_num
mat.append(row)
for row in mat:
for d in row:
print('%2d' % d, end=' ')
print()
result1 = cycle_print1(mat)
result2 = cycle_print2(mat)
print(result1)
print(result2)
print('----------------------------------------------------')
if __name__ == '__main__':
test_cycle_print(0, 0)
test_cycle_print(0, 1)
test_cycle_print(1, 0)
test_cycle_print(1, 1)
test_cycle_print(1, 2)
test_cycle_print(2, 1)
test_cycle_print(2, 2)
test_cycle_print(4, 4)
test_cycle_print(4, 3)
test_cycle_print(3, 4)
test_cycle_print(6, 6)
test_cycle_print(6, 4)
test_cycle_print(4, 6)
结果
[]
[]
----------------------------------------------------
[]
[]
----------------------------------------------------
[]
[]
----------------------------------------------------
1
[1]
[1]
----------------------------------------------------
1 2
[1, 2]
[1, 2]
----------------------------------------------------
1
2
[1, 2]
[1, 2]
----------------------------------------------------
1 2
3 4
[1, 2, 4, 3]
[1, 2, 4, 3]
----------------------------------------------------
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
----------------------------------------------------
1 2 3
4 5 6
7 8 9
10 11 12
[1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8]
[1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8]
----------------------------------------------------
1 2 3 4
5 6 7 8
9 10 11 12
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
----------------------------------------------------
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
[1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
[1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
----------------------------------------------------
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
[1, 2, 3, 4, 8, 12, 16, 20, 24, 23, 22, 21, 17, 13, 9, 5, 6, 7, 11, 15, 19, 18, 14, 10]
[1, 2, 3, 4, 8, 12, 16, 20, 24, 23, 22, 21, 17, 13, 9, 5, 6, 7, 11, 15, 19, 18, 14, 10]
----------------------------------------------------
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
[1, 2, 3, 4, 5, 6, 12, 18, 24, 23, 22, 21, 20, 19, 13, 7, 8, 9, 10, 11, 17, 16, 15, 14]
[1, 2, 3, 4, 5, 6, 12, 18, 24, 23, 22, 21, 20, 19, 13, 7, 8, 9, 10, 11, 17, 16, 15, 14]
----------------------------------------------------