简体   繁体   中英

how to reduce memory usage this program recursive

This function return array of number n digit but it use memory very much. How can I improve this func for reduce memory

def think(n=5):
    if n == 1:
        return ([str(i) for i in range(1,10)])
    else :
        result = []
        # result1 = think(n-1)
        for i in think(n-1):
            for j in range(10):
                result.append(i+str(j))
        return result

You can start by removing the result list out of recursion. Because for every recursive call you are creating a new result list. Instead, you should create it once before calling the recursive function and keep passing it as function parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM