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

其他题目

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

相关推荐
wen__xvn2 小时前
算法基础集训第19天:广度优先搜索
算法·宽度优先
这就是佬们吗2 小时前
力扣---leetcode48
java·笔记·后端·算法·leetcode·idea
薛不痒2 小时前
项目:矿物分类(训练模型)
开发语言·人工智能·python·学习·算法·机器学习·分类
被星1砸昏头2 小时前
C++与Node.js集成
开发语言·c++·算法
MicroTech20252 小时前
微算法科技(NASDAQ :MLGO)开发基于QML的入侵检测识别系统(QML-IDS),强化网络安全防护
科技·算法·web安全
SR_shuiyunjian2 小时前
Python第一次作业
开发语言·python·算法
期末考复习中,蓝桥杯都没时间学了2 小时前
力扣刷题记录3
算法·leetcode·职场和发展
云深麋鹿2 小时前
一.算法复杂度
c语言·开发语言·算法