70. 爬楼梯
class Solution {
public:
int climbStairs(int n) {
if(n == 1){
return 1;
}
if(n == 2){
return 2;
}
//return climbStairs(n - 2) + climbStairs(n - 1);
vector<int> nums(n+1,0);
nums[1] = 1;
nums[2] = 2;
for(int i = 3;i<=n;i++){
nums[i] = nums[i-1] + nums[i-2];
}
return nums[n];
}
};
746. 使用最小花费爬楼梯
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
if(n == 0){
return 0;
}else if(n == 1){
return cost[0];
}
vector<int> nums(n,0);
nums[0] = cost[0];
nums[1] = cost[1];
for(int i = 2;i<n;i++){
nums[i] = min(nums[i-1],nums[i-2])+cost[i];
}
return min(nums[n-1],nums[n-2]);
}
};