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

其他题目

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

相关推荐
Yzzz-F2 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业3 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn3 小时前
Java Set集合相关知识点
java·开发语言·算法
生成论实验室4 小时前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
AI科技星4 小时前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
科研前沿5 小时前
镜像孪生VS视频孪生核心技术产品核心优势
大数据·人工智能·算法·重构·空间计算
水蓝烟雨5 小时前
1931. 用三种不同颜色为网格涂色
算法·leetcode
晨曦夜月5 小时前
map与unordered_map区别
算法·哈希算法
图码6 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻
handler016 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法