我是Python的新手,在将其应用于同一列表时会遇到列表理解问题。
代码是
# both grid and dp are already defined as the following types:
# grid: List[List[int]]
# dp: List[int]
for j in range(0, len(grid)):
dp = [v + dp[i] if i==0 else v + min(dp[i-1],dp[i]) for i,v in enumerate(grid[j])]
我认为这等同于
for j in range(0, len(g)):
for i,v in enumerate(g[j]):
dp[i] = v + dp[i] if i==0 else v + min(dp[i-1],dp[i])
但是结果不同。 样本输入:
dp = [0,5,5]
grid = [[1,3,1]]
列出理解结果:dp = [1,3,6]
for
循环结果:dp = [1,4,5]
(预期)
有什么建议吗?