#50
Pow(x, n)
medium· Math & Geometryruns: 0Implement pow(x, n) which calculates x raised to the power n. The implementation must run in O(log n) time.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 272
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
x = 1 / x
n = -n
result = 1.0
while n:
if n & 1:
result *= x
x *= x
n >>= 1
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.