一爬楼梯
题目描述
class Solution {
public:
int climbStairs(int n) {
int a = 1 , b = 2;
int c = 0;
if(n==1) return a;
else if(n==2) return b;
for(int i=2;i<n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}
};
[1,100,1,1,1,100,1,1,100,1,5,10,44,87,910,154,10,23]
二 使用最小花费爬楼梯
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int>toallCost(cost.size()+1);
for(int i=2;i<=cost.size();i++)
toallCost[i] = (toallCost[i-1]+cost[i-1]>toallCost[i-2]+cost[i-2]?toallCost[i-2]+cost[i-2]:toallCost[i-1]+cost[i-1]);
return toallCost[toallCost.size()-1];
}
};