100197. Three Steps Problem LCCI
2026/1/12小于 1 分钟约 188 字
100197. Three Steps Problem LCCI
难度: Easy
题目描述
A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. The result may be large, so return it modulo 1000000007.
Example1:
Input: n = 3 Output: 4
Example2:
Input: n = 5 Output: 13
Note:
1 <= n <= 1000000
解题思路
代码实现
解决方案
java
class Solution {
public int waysToStep(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
if (n == 3) {
return 4;
}
int [] dp = new int[n+1];
dp[0]=1;
dp[1]=1;
dp[2]=2;
for(int i=3;i<=n;i++){
dp[i] = ((dp[i-1]+dp[i-2])%1000000007+dp[i-3])%1000000007;
}
return dp[n]%1000000007;
}
}