leetcode Climbing Stairs python

Climbing Stairs

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?

python code:

class Solution:
# @param {integer} n
# @return {integer}
def climbStairs(self, n):
  if n<=0:        #illegal input
    return 0
  else:
    if n==1:
      return 1
    if n==2:
      return 2
    a,b=1,2
    for i in range(3,n+1):
      a,b=b,a+b   #斐波拉契数列
    return b

posted @ 2015-06-14 06:39  轻抚离殇  阅读(187)  评论(0编辑  收藏  举报