class Solution int minCostClimbingStairsListint cost Listint dp=Listintfilled20; forint i=2;icostlength;i++ dpaddmindpi-1+costi-1dpi-2+costi-2; return dpdplength-1; 第4行执行出错
The error in line 4 is due to the fact that the List class in C# does not have a method called "filled". To fix this error, you can initialize the dp list with two elements and then use a for loop to add the remaining elements. Here's the modified code:
class Solution {
int minCostClimbingStairs(List<int> cost){
List<int> dp = new List<int>(){0, 0};
for(int i = 2; i < cost.Count; i++) dp.Add(Math.Min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]));
return dp[dp.Count-1];
}
}
``
原文地址: https://www.cveoy.top/t/topic/izoR 著作权归作者所有。请勿转载和采集!