The error is occurring because you are trying to add elements to a fixed-length list dp. In Dart, a fixed-length list cannot be modified after it is created.

To fix the error, you can change the declaration of dp to a growable list using the List() constructor. Here's the modified code:

class Solution {
    int minCostClimbingStairs(List<int> cost){
        List<int> dp = List<int>.from([0, 0]);
        for(int i=2; i<cost.length; i++) {
            dp.add(min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]));
        }
        return dp[dp.length-1];
    }
}
``
class Solution int minCostClimbingStairsListint cost 	 Listint dp=Listintfilled20;			forint i=2;icostlength;i++ dpaddmindpi-1+costi-1dpi-2+costi-2;			return dpdplength-1; 第4行执行出错DartCanno

原文地址: https://www.cveoy.top/t/topic/izo5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录