Skip to content

Instantly share code, notes, and snippets.

@anish000kumar
Created August 29, 2020 23:35
Show Gist options
  • Save anish000kumar/8108d4289ea2b9d9530de88e3361cd51 to your computer and use it in GitHub Desktop.
Save anish000kumar/8108d4289ea2b9d9530de88e3361cd51 to your computer and use it in GitHub Desktop.
Pow(x, n)
def myPow(self, x: float, n: int) -> float:
ans, power = 1, abs(n)
while power:
if power % 2 == 0:
x *= x
power //= 2
else:
ans *= x
power -= 1
return (1/ans) if n<0 else ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment