Python "1line"?


  • 0
    T
    from functools import wraps
    
    def memo(f):
        cache = {}
        @wraps(f)
        def wrap(*args):
            if args not in cache:
                cache[args] = f(*args)
            return cache[args]
        return wrap
    
    class Solution(object):
        @memo
        def numTrees(self, n):
            return 1 if n<= 1 else sum(self.numTrees(i)*self.numTrees(n-1-i) for i in range(n))
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.