三步问题。有个小孩正在上楼梯,楼梯有n阶台阶,小孩一次可以上1阶、2阶或3阶。实现一种方法,计算小孩有多少种上楼梯的方式。结果可能很大,你需要对结果模1000000007。
示例1:
输入:n = 3
输出:4
说明: 有四种走法
示例2:
输入:n = 5
输出:13
提示:
- n范围在[1, 1000000]之间
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int waysToStep(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
if (n == 3) {
return 4;
}
int a1 = 1;
int a2 = 2;
int a3 = 4;
int cur = 0;
for (int i = 4; i <= n; i++) {
cur = ((a1 + a2) % 1000000007 + a3) % 1000000007;
a1 = a2;
a2 = a3;
a3 = cur;
}
return cur;
}
}
//leetcode submit region end(Prohibit modification and deletion)