题目:
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?
题意:
爬楼梯,每次只能爬1级或2级,问爬n级楼梯一共有多少种爬法
代码:
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
stepone = 0
steptwo = 1
result = 0
for i in range(n) :
result = stepone + steptwo
stepone = steptwo
steptwo = result
return result
笔记:
本来用递归做,结果超时,参考网上大神的做法
http://blog.csdn.net/wangyaninglm/article/details/45369627