[leetcode] 70 Climbing Stairs

from functools import wraps
def memo(func):
    cache={}
    @wraps(func)
    def wrap(self,*args):

        if args not in cache:
            cache[args]=func(self,*args) #!!!!!!!!!!!!
        return cache[args]
        
    return wrap


class Solution:
    # @param {integer} n
    # @return {integer}
    @memo
    def climbStairs(self, n):

        if n<4:
            return n
        return self.climbStairs(n-1)+self.climbStairs(n-2)
        

 

class Solution:
    # @param {integer} n
    # @return {integer}
    
    def climbStairs(self, n):
        a = 1
        b = 2
        if n<3:
            return n
        for i in range(n-2):
            a,b = b,a+b
        return b

        

 

posted @ 2015-06-10 16:23  程序员阿力  阅读(236)  评论(0编辑  收藏  举报