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

其他题目

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

相关推荐
2301_764441332 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
东北洗浴王子讲AI2 小时前
GPT-5.4辅助算法设计与优化:从理论到实践的系统方法
人工智能·gpt·算法·chatgpt
Billlly3 小时前
ABC 453 个人题解
算法·题解·atcoder
玉树临风ives3 小时前
atcoder ABC 452 题解
数据结构·算法
feifeigo1233 小时前
基于马尔可夫随机场模型的SAR图像变化检测源码实现
算法
fengfuyao9854 小时前
基于STM32的4轴步进电机加减速控制工程源码(梯形加减速算法)
网络·stm32·算法
无敌昊哥战神5 小时前
深入理解 C 语言:巧妙利用“0地址”手写 offsetof 宏与内存对齐机制
c语言·数据结构·算法
小白菜又菜5 小时前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
Proxy_ZZ05 小时前
用Matlab绘制BER曲线对比SPA与Min-Sum性能
人工智能·算法·机器学习
黎阳之光5 小时前
黎阳之光:以视频孪生领跑全球,赋能数字孪生水利智能监测新征程
大数据·人工智能·算法·安全·数字孪生