Why the brute-force solution gets MemoryError?


  • 0
    Z
    class Solution(object):
    def myPow(self, x, n):
        """
        :type x: float
        :type n: int
        :rtype: float
        """
        mult=1
        for i in range(abs(n)):
            mult=mult*x
        if n<0:
            return 1.0/mult
        else:
            return mult
    

    I am a new beginner for Python. I do know this solution is the most inefficient way, but I just want to know what does the error MemoryError mean?
    Thanks in advance.


  • 1
    L

    range(n) returns an array: [0, 1, 2, ..., n-1]; it could be larger than the size of your computer's memory if n is big. Use xrange(n) instead.


Log in to reply
 

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