鱼C论坛

 找回密码
 立即注册
查看: 4273|回复: 7

[已解决]列表矩阵

[复制链接]
发表于 2022-4-18 18:24:34 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Ari小虎鱼 于 2022-4-18 18:25 编辑
  1. matrix = [[1,2,3,4],
  2.           [5,6,7,8],
  3.           [9,10,11,12]]

  4. #初始化
  5. top = 0
  6. bottom = len(matrix)
  7. right = len(matrix[0])
  8. left = 0
  9. spiral = []

  10. #遍历一周
  11. while True:
  12.     for each in range(left,right):
  13.         spiral.append(matrix[top][each])

  14.     for each in range((top+1),bottom):
  15.         spiral.append(matrix[each][right-1])

  16.     for each in range(right-2,left-1,-1):
  17.         spiral.append(matrix[bottom-1][each])

  18.     for each in range(bottom-2,top,-1):
  19.         spiral.append(matrix[each][left])

  20.     left += 1
  21.     right -=1
  22.     top += 1
  23.     bottom -= 1

  24.     if left > right or top > bottom:
  25.         break
  26.    
  27. #打印
  28. print(spiral)
复制代码

代码运行整体没问题,就是结尾会再返回
  1. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, 6]
  2. >>>
复制代码

请大佬看看这个应该怎么改?
最佳答案
2022-4-21 16:59:07
你可以把第三个和第四个for循环的逻辑改一改
第三个不用到最左边的元素,也就是下标为0的元素
第四个循环就可以从最左边的元素往上走
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-18 18:32:55 From FishC Mobile | 显示全部楼层
就是结尾会再返回
这句话是啥意思???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-18 21:56:27 | 显示全部楼层
可以个笨办法来解决,就会发现问题出现在第三个for循环
  1. matrix = [[1, 2, 3, 4],
  2.           [5, 6, 7, 8],
  3.           [9, 10, 11, 12]]

  4. # 初始化
  5. top = 0
  6. bottom = len(matrix)  # 3
  7. right = len(matrix[0])  # 4
  8. left = 0
  9. spiral = []

  10. # 遍历一周
  11. while True:
  12.     for each in range(left, right):  # 04 13
  13.         spiral.append(matrix[top][each])
  14.         print(matrix[top][each])

  15.     for each in range((top+1), bottom):  # 13 33pass
  16.         spiral.append(matrix[each][right-1])
  17.         print(matrix[each][right-1])

  18.     for each in range(right-2, left, -1):  # 2-1 10  !!!
  19.         spiral.append(matrix[bottom-1][each])
  20.         print(matrix[bottom-1][each], right, left)

  21.     for each in range(bottom-1, top, -1):  # 10 00
  22.         spiral.append(matrix[each][left])
  23.         print(matrix[each][left])

  24.     left += 1
  25.     right -= 1
  26.     top += 1
  27.     bottom -= 1

  28.     if left > right or top > bottom:
  29.         break

  30. # 打印
  31. print(spiral)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-20 12:20:57 | 显示全部楼层
大马强 发表于 2022-4-18 21:56
可以个笨办法来解决,就会发现问题出现在第三个for循环

嗯嗯,就是我知道问题在第三个循环但是找不出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-21 00:02:20 | 显示全部楼层
Ari小虎鱼 发表于 2022-4-20 12:20
嗯嗯,就是我知道问题在第三个循环但是找不出来

你现在知道你的问题了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-21 12:23:17 | 显示全部楼层
大马强 发表于 2022-4-21 00:02
你现在知道你的问题了吗

就是它顺时针走一圈,走到最内圈的时候会再向左走,相当于又走了一圈
不知道怎么改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-21 16:59:07 | 显示全部楼层    本楼为最佳答案   
你可以把第三个和第四个for循环的逻辑改一改
第三个不用到最左边的元素,也就是下标为0的元素
第四个循环就可以从最左边的元素往上走
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-4-24 17:26:16 | 显示全部楼层
wp231957 发表于 2022-4-18 18:32
就是结尾会再返回
这句话是啥意思???

emm就是结果是[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, 6]输出7后又输出6
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 04:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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