基础算法集训第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;
    }
};

其他题目

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

相关推荐
你撅嘴真丑4 小时前
第九章-数字三角形
算法
uesowys4 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder4 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮4 小时前
AI 视觉连载1:像素
算法
智驱力人工智能4 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥5 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风5 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風6 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT066 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠6 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法