Skip to content

Instantly share code, notes, and snippets.

@munguial
Created July 16, 2020 16:50
Show Gist options
  • Save munguial/da67c5044c9dc927d0a9a7270ec294d1 to your computer and use it in GitHub Desktop.
Save munguial/da67c5044c9dc927d0a9a7270ec294d1 to your computer and use it in GitHub Desktop.
July - Day 16 - Pox(x, n)
# 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