Created
August 29, 2020 23:35
-
-
Save anish000kumar/8108d4289ea2b9d9530de88e3361cd51 to your computer and use it in GitHub Desktop.
Pow(x, n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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