Python generator (and decorator)

  • 0
    class Solution:
        # @param an integer
        # @return a list of string
        # F(n) = {“(“ + F(i) + “)” + F(n-i-1) for i=0..n-1}
        def generateParenthesis(self, n):
            #from functools import lru_cache        ## Can be memoized with
            #@lru_cache                             ##  <--  this decorator
            def recursive(n):
                if not n: yield ''
                    for i in range(n):
                        for outer in recursive(n-i-1):
                            for inner in recursive(i):
                                yield '({}){}'.format(inner, outer)
            return list(recursive(n))

    Functools module is not allowed on leetcode.
    Lru_cache enables lru function and can take an optional parameter to specify cache size (1024 bytes by default).


    If maxsize=None, lru function is disabled and cache can grow without bound.

Log in to reply

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