你在爬楼梯,需要n步才能爬到楼梯顶部
每次你只能向上爬1步或者2步。有多少种方法可以爬到楼梯顶部?
# # # @param n int整型 # @return int整型 # class Solution: def climbStairs(self , n ): # write code here dp=[0,1,2] for i in range(3,n+1): dp.append(dp[i-1]+dp[i-2])#把所有结果保存在一个数组中 return dp[n]
每一层的方法,就是前一层+前两层的方法!