基础算法集训第03天:递推

目录

[119. 杨辉三角 II - 力扣(LeetCode)](#119. 杨辉三角 II - 力扣(LeetCode))

[70. 爬楼梯 - 力扣(LeetCode)](#70. 爬楼梯 - 力扣(LeetCode))

[509. 斐波那契数 - 力扣(LeetCode)](#509. 斐波那契数 - 力扣(LeetCode))

[1137. 第 N 个泰波那契数 - 力扣(LeetCode)](#1137. 第 N 个泰波那契数 - 力扣(LeetCode))

[LCR 126. 斐波那契数 - 力扣(LeetCode)](#LCR 126. 斐波那契数 - 力扣(LeetCode))

[面试题 08.01. 三步问题 - 力扣(LeetCode)](#面试题 08.01. 三步问题 - 力扣(LeetCode))

[LCR 127. 跳跃训练 - 力扣(LeetCode)](#LCR 127. 跳跃训练 - 力扣(LeetCode))

其他题目


119. 杨辉三角 II - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<vector<int>> s(rowIndex+1);
        for(int i=0;i<rowIndex+1;i++){
            s[i].resize(i+1);
            s[i][0]=1;
            s[i][i]=1;
            for(int j=1;j<i;j++){
                s[i][j]=s[i-1][j-1]+s[i-1][j];
            }
        }
        return s[rowIndex];
    }
};

优化

cpp 复制代码
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> a,b;
        for(int i=0;i<rowIndex+1;i++){
            a.resize(i+1);
            a[0]=1;
            a[i]=1;
            for(int j=1;j<i;j++){
                a[j]=b[j-1]+b[j];
            }
            b=a;
        }
        return b;
    }
};

优化

cpp 复制代码
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> s(rowIndex+1);
        s[0]=1;
        for(int i=1;i<=rowIndex;i++){
            for(int j=i;j>0;j--){
                s[j]+=s[j-1];
            }
        }
        return s;
    }
};

法二

cpp 复制代码
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> s(rowIndex+1);
        s[0]=1;
        for(int i=1;i<=rowIndex;i++){
            s[i]=1ll*s[i-1]*(rowIndex-i+1)/i;
        }
        return s;
    }
};

70. 爬楼梯 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int climbStairs(int n) {
        if(n<=3)return n;
        int a=1;
        int b=2;
        for(int i=3;i<=n;i++){
            int t=a+b;
            a=b;
            b=t;
        }
        return b;
    }
};

509. 斐波那契数 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int fib(int n) {
        if(n<=1)return n;
        int a=0;
        int b=1;
        for(int i=2;i<=n;i++){
            int t=a+b;
            a=b;
            b=t;
        }
        return b;
    }
};

1137. 第 N 个泰波那契数 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int tribonacci(int n) {
        if(n==0)return 0;
        if(n==1)return 1;
        if(n==2)return 1;
        int a=0;
        int b=1;
        int c=1;
        for(int i=3;i<=n;i++){
            int C=a+b+c;
            int B=c;
            int A=b;
            a=A;
            b=B;
            c=C;
        }
        return c;
    }
};

LCR 126. 斐波那契数 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int fib(int n) {
        if(n==0)return 0;
        if(n==1)return 1;
        int a=0;
        int b=1;
        for(int i=2;i<=n;i++){
            int t=(a+b)%1000000007;
            a=b%1000000007;
            b=t%1000000007;
        }
        return b;
    }
};

面试题 08.01. 三步问题 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int waysToStep(int n) {
        if(n==1)return 1;
        if(n==2)return 2;
        if(n==3)return 4;
        int a=1;
        int b=2;
        int c=4;
        for(int i=4;i<=n;i++){
            int C=((long long)a+(long long)b+(long long)c)%1000000007;
            int B=c%1000000007;
            int A=b%1000000007;
            a=A;
            b=B;
            c=C;
        }
        return c;
    }
};

LCR 127. 跳跃训练 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int trainWays(int num) {
        if(num==0)return 1;
        if(num==1)return 1;
        if(num==2)return 2;
        int a=1;
        int b=2;
        for(int i=3;i<=num;i++){
            int t=(a+b)%1000000007;
            a=b%1000000007;
            b=t;
        }
        return b;
    }
};

其他题目

知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具

相关推荐
草履虫建模8 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq10 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq11 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化
爱吃rabbit的mq11 小时前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)12 小时前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi12 小时前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
不能隔夜的咖喱13 小时前
牛客网刷题(2)
java·开发语言·算法
VT.馒头13 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
进击的小头13 小时前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机
咖丨喱14 小时前
IP校验和算法解析与实现
网络·tcp/ip·算法