目录
[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;
}
};
其他题目
知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具
