[英]Why it returns IndexError: list index out of range when index exists?
dp
只有 1 行包含(rows+1) * (cols+1)
零,而不是包含cols+1
零的rows+1
行。 因此,每當您使用i >= 1
訪問dp[i]
,您都會收到錯誤消息。
它應該是:
dp = [[0 for i in range(cols+1)] for j in range(rows+1)]
或者更簡單
dp = [[0] * (cols+1) for _ in range(rows+1)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.