题目描述
有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或者二级,要走上m级,共有多少走法?注:规定从一级到一级有0种走法。
给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100。为了防止溢出,请返回结果Mod 1000000007的值。
测试样例:
3
返回:2
题目来源:京东
class GoUpstairs:
def countWays(self, n):
# write code here
if n <= 0:
return 0
dp = [0] * 111
dp[1] = 0
dp[2] = 1
dp[3] = 2
for i in range(4, n + 1):
dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007
return dp[n]
if __name__ == "__main__":
a = GoUpstairs()
print a.countWays(3)
print a.countWays(30)