鱼C论坛

 找回密码
 立即注册
查看: 165|回复: 2

求问这两段代码有什么区别,为什么第一段会报错

[复制链接]
发表于 2024-4-14 02:28:19 | 显示全部楼层 |阅读模式

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

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

x
  1. matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
  2. rows = len(matrix)
  3. cols = len(matrix[0])
  4. top = 0
  5. right = cols - 1
  6. left = 0
  7. bottom = rows - 1
  8. result = []
  9. while left <= right and top <= bottom:
  10.     for col in range(left,right + 1):
  11.         result.append(matrix[top][col])
  12.     for row in range(top + 1,bottom + 1):
  13.         result.append(matrix[row][right])
  14.     if left < right and top < bottom:   
  15.         for col in range(right - 1,left,-1):
  16.             result.append(matrix[bottom][col])
  17.         for row in range(bottom - 1,top - 1):
  18.             result.append(matrix[row][left])
  19.     left += 1
  20.     top -= 1
  21.     bottom += 1
  22.     right -= 1

  23. print(result)
复制代码


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

这两段有什么区别,第一段为什么会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-14 02:28:29 | 显示全部楼层
第一段代码报错是因为在创建matrix列表时,没有给出具体的元素值。这里用了多个逗号,表示缺少元素值,导致语法错误。

改正后的代码应该是:
matrix = [[], [], []]
rows = len(matrix)
cols = len(matrix[0])
top = 0
right = cols - 1
left = 0
bottom = rows - 1
result = []
while left <= right and top <= bottom:
    for i in range(left, right + 1):
        result.append(matrix[top])
    top += 1
    for i in range(top, bottom + 1):
        result.append(matrix[right])
    right -= 1
    if top <= bottom:
        for i in range(right, left - 1, -1):
            result.append(matrix[bottom])
        bottom -= 1
    if left <= right:
        for i in range(bottom, top - 1, -1):
            result.append(matrix[left])
        left += 1
这段代码是用于遍历二维矩阵的边界,并按照顺时针方向将边界上的元素添加到result列表中。通过四个变量来记录边界的位置,然后根据边界的位置遍历相应的元素并添加到result列表中。最后返回result列表。

第二段代码因为未提供完整,所以无法进行比较。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-14 09:28:37 | 显示全部楼层
本帖最后由 星许Sky 于 2024-4-14 09:30 编辑

我认为第一段代码既然和第二段代码相差不大,说明你已大致了解第二段代码的原理和作用,
是故在此不再进行解释(主要是我比较懒得思考,我高中都还没有毕业)。
①在while循环体内的if判断中的for row in range()语句中的参数有点问题。
if判断下的从上往下遍历代码原理同从右往左遍历。
则其中第二range的参数值应为:bottom, top, -1.
在Python的range语句中的第三参数表示的是步长:
若其为正数,则按列表0到n-1正序排列;
若其为负数,则按列表n-1到0逆序排列。
同时
②while循环体尾,变量的重新赋值有误。
此处,变量的重新赋值应遵循从上往下赋值同理于从左往右赋值。
故应将top -= 1改为top += 1
bottom += 1改为bottom -= 1.
才疏学浅,如有错误,还请指正。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 00:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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