力扣第31天----第509题、第70题、第746题

力扣第31天----第509题、第70题、第746题

一、第509题–斐波那契数

​ 记住这五步,今天就够了–dp数组含义、递推公式、初始化、便利顺序、打印dp数组。

class Solution {
public:
    int fib(int n) {
        if (n < 2) return n;
        int f0 = 0;
        int f1 = 1;
        int result = 0;
        while(n > 1){
            result = f0 + f1;
            f0 = f1;
            f1 = result;
            n--;
        }
        return result;
    }
};

二、第70题–爬楼梯

​ 记住这五步,今天就够了–dp数组含义、递推公式、初始化、便利顺序、打印dp数组。

class Solution {
public:
    int climbStairs(int n) {
        if (n <3 ) return n;
        int n1 = 1;
        int n2 = 2;
        int result = 0;
        while(n>2){
            result = n1 + n2;
            n1 = n2;
            n2 = result;
            n--;
        }
        return result;
    }
};

三、第746题–使用最小花费爬楼梯

​ 记住这五步,今天就够了–dp数组含义、递推公式、初始化、便利顺序、打印dp数组。

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int i0 = 0;
        int i1 = 0;
        int result = 0;
        for (int i = 2; i <= cost.size(); i++){
            result = min(i0 + cost[i-2], i1 + cost[i-1]);
            i0 = i1;
            i1 = result;
        }
        return result;

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值