graph = [[8],[4,9],[9,2,1],[3,8,5,5],[5,6,3,7,6],[3,8,5,5],[9,2,1],[4,9],[8]]for i in range(len(graph)-2, -1, -1): for j in range(len(graph[i])): graph[i][j] += max(graph[i+1][j], graph[i+1][j+1])print(max(graph[0]))为什么显示index out of range,如何修改
时间: 2023-06-10 20:07:07 浏览: 126
VMR9_Test.rar_ vmr9_ VMR9_vmr9
这个错误是因为在代码中使用了`graph[i 1]`而不是`graph[i+1]`。`i 1`被解释为`i`和`1`之间的空格,而不是`i+1`。
以下是修改后的代码:
```
graph = [[8],[4,9],[9,2,1],[3,8,5,5],[5,6,3,7,6],[3,8,5,5],[9,2,1],[4,9],[8]]
for i in range(len(graph)-2, -1, -1):
for j in range(len(graph[i])):
graph[i][j] = max(graph[i+1][j], graph[i+1][j+1])
print(max(graph[0]))
```
这个代码将输出最大的路径和。
阅读全文