class Solution {
public:
int waysToStep0(int n) {
if(n < 0) return -1;
if(n == 0) return 0;
if(n == 1) return 1;
if(n == 2) return 2;
vector<int> dp(n+1, 0);
dp[1] = 1;
dp[2] = 2;
dp[3] = 4;
for(int i =4; i <=n; i++){
dp[i] = ( (dp[i-1]%1000000007+dp[i-2]%1000000007)%1000000007 +dp[i-3]%1000000007)%1000000007;
}
return dp[n];
}
int waysToStep(int n) {
if(n < 0) return -1;
if(n == 0) return 0;
if(n == 1) return 1;
if(n == 2) return 2;
int dp1 = 1;
int dp2 = 2;
int dp3 = 4;
for(int i =4; i <=n; i++){
int temp = ( (dp1%1000000007+dp2%1000000007)%1000000007 +dp3%1000000007)%1000000007;
dp1 = dp2;
dp2 = dp3;
dp3 = temp;
}
return dp3;
}
};
爬楼梯问题,注意相加别溢出了