Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Save $650 on Qlik Connect, Dec 1 - 7, our lowest price of the year. Register with code CYBERWEEK: Register
cancel
Showing results for 
Search instead for 
Did you mean: 
asolbri2
Contributor
Contributor

Memory Limit Exceeded-Need Help

Hi all. I have been working on the pow(x,n) question num50 on leetcode. However, I meet a memory limit exceeded error when the testcase of x =0.00001 n =2147483647 comes up. Originally, I just had two For loops running(1 for multiplication 1 for division). I changed it to a single while loop as I thought maybe the code wasnt optimised enough. As of right now though I have no idea what is causing it.

class Solution(object):
def myPow(self, x, n):
i=1
isnegative=False
orivalue=x
if (n==0):
return 1
if (n<0):
isnegative=True
n=abs(n)
while (i<n): #stop when it has reached the n
if (2*i<=n): #can still square itself
x=x*x
i=i+i #power increases 1+1=2 2+2=4
else: #have to multiply with ori value
x=x*orivalue
i=i+1
if isnegative==True:
x=1/x
return x

Labels (2)
0 Replies