鱼C论坛

 找回密码
 立即注册
查看: 3536|回复: 4

[已解决]python 代码求助 按照顺时针螺旋顺序输出矩阵中的所有元素

[复制链接]
发表于 2022-12-8 09:09:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 成成是我 于 2022-12-12 08:36 编辑

请按照顺时针螺旋顺序输出矩阵中的所有元素

  1. >>> matrix = [[1, 2, 3, 4],
  2. ...           [5, 6, 7, 8],
  3. ...           [9, 10, 11, 12]]

  4. 那么将输出:
  5. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]


  6. 代码清单:
  7. matrix = [[1, 2, 3, 4],
  8.           [5, 6, 7, 8],
  9.           [9, 10, 11, 12]]
  10.    
  11. rows = len(matrix)
  12. cols = len(matrix[0])
  13.    
  14. left = 0
  15. right = cols - 1
  16. top = 0
  17. bottom = rows - 1
  18.    
  19. result = []
  20.    
  21. while left <= right and top <= bottom:
  22.     # 从左往右遍历
  23.     for col in range(left, right + 1):
  24.         result.append(matrix[top][col])
  25.    
  26.     # 从上往下遍历
  27.     for row in range(top + 1, bottom + 1):
  28.         result.append(matrix[row][right])
  29.    
  30.     if left < right and top < bottom:
  31.         # 从右往左遍历
  32.         for col in range(right - 1, left, -1):
  33.             result.append(matrix[bottom][col])
  34.    
  35.         # 从下往上遍历
  36.         for row in range(bottom, top, -1):
  37.             result.append(matrix[row][left])
  38.    
  39.     left = left + 1
  40.     right = right - 1
  41.     top = top + 1
  42.     bottom = bottom - 1
  43.    
  44. print(result)
复制代码



请大佬解释下红字部分:
1.right 和botton 初始值为什么要-1?
2.请解释下循环范围
感谢感谢!
最佳答案
2022-12-9 17:20:07
  1. # 从左往右遍历,首先是第一行既top=0保持不变,matrix[top]=[1, 2, 3, 4],col从0到right值就可以把matrix[top]的所有值取出来,既1, 2, 3, 4
  2. for col in range(left, right + 1):
  3.     result.append(matrix[top][col])   #行是确定的,即为top   
  4.     # 从上往下遍历,这个时候从最右侧这列开始(最右侧这一列的第二个元素开始,因为这一列第一个元素既4已经在上面被取出来了),这一列的元素来着不同的matrix[row]的最后一个元素。既 8, 12
  5. for row in range(top + 1, bottom + 1):
  6.     result.append(matrix[row][right])   #列是确定的,即为right  
  7. if left < right and top < bottom:
  8.         # 从右往左遍历,这个时候从最下面一行开始(最下面一行的倒数第二个元素开始,因为这一行的最后第一个元素既12已经在上面被取出来了),既11, 10, 9。
  9.     for col in range(right - 1, left, -1):
  10.         result.append(matrix[bottom][col])  #行是确定的,即为botton
  11.         # 从下往上遍历
  12.     for row in range(bottom, top, -1):
  13.         result.append(matrix[row][left])    #列是确定的,即为left
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-8 11:48:50 | 显示全部楼层
1、right表示最右侧一列(序号从left=0开始),bottom 表示最下面一行(序号从top=0开始)
2、每一个for循环都是先确定行或者列号,然后遍历剩下的数
  1. for col in range(left, right + 1):
  2.     result.append(matrix[top][col])   #行是确定的,即为top   
  3.     # 从上往下遍历
  4. for row in range(top + 1, bottom + 1):
  5.     result.append(matrix[row][right])   #列是确定的,即为right  
  6. if left < right and top < bottom:
  7.         # 从右往左遍历
  8.     for col in range(right - 1, left, -1):
  9.         result.append(matrix[bottom][col])  #行是确定的,即为botton
  10.         # 从下往上遍历
  11.     for row in range(bottom, top, -1):
  12.         result.append(matrix[row][left])    #列是确定的,即为left
复制代码

   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 17:20:07 | 显示全部楼层    本楼为最佳答案   
  1. # 从左往右遍历,首先是第一行既top=0保持不变,matrix[top]=[1, 2, 3, 4],col从0到right值就可以把matrix[top]的所有值取出来,既1, 2, 3, 4
  2. for col in range(left, right + 1):
  3.     result.append(matrix[top][col])   #行是确定的,即为top   
  4.     # 从上往下遍历,这个时候从最右侧这列开始(最右侧这一列的第二个元素开始,因为这一列第一个元素既4已经在上面被取出来了),这一列的元素来着不同的matrix[row]的最后一个元素。既 8, 12
  5. for row in range(top + 1, bottom + 1):
  6.     result.append(matrix[row][right])   #列是确定的,即为right  
  7. if left < right and top < bottom:
  8.         # 从右往左遍历,这个时候从最下面一行开始(最下面一行的倒数第二个元素开始,因为这一行的最后第一个元素既12已经在上面被取出来了),既11, 10, 9。
  9.     for col in range(right - 1, left, -1):
  10.         result.append(matrix[bottom][col])  #行是确定的,即为botton
  11.         # 从下往上遍历
  12.     for row in range(bottom, top, -1):
  13.         result.append(matrix[row][left])    #列是确定的,即为left
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-9 17:29:07 | 显示全部楼层

嗯嗯,明白了,要把之前取出的数摘出去,谢谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 20:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表