Created
July 16, 2020 16:50
-
-
Save munguial/da67c5044c9dc927d0a9a7270ec294d1 to your computer and use it in GitHub Desktop.
July - Day 16 - Pox(x, n)
This file contains hidden or 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
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3392/ | |
class Solution: | |
def myPow(self, x: float, n: int) -> float: | |
if n == 0: | |
return 1 | |
if n == 1: | |
return x | |
if n < 0: | |
x = 1 / x | |
n = n * -1 | |
half = self.myPow(x, n // 2) | |
if n % 2 == 0: | |
return half * half | |
else: | |
return half * half * x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment