题目描述:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
思路:
本题是一道典型的动态规划问题, 只有一步是方法为一种,两步是方法为两种。当n>2时,dp[n] = dp[n - 1] + dp[n - 2]。
AC代码:
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
dp = [0] * n
for i in range(n):
if i == 0:
dp[i] = 1
elif i == 1:
dp[i] = 2
else:
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n - 1]