Skip to content

Commit

Permalink
Create Pow(x, n).py
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh048 authored Nov 29, 2023
1 parent 80d3d7d commit de94bab
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Pow(x, n).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def myPow(self, x, n):
if n == 0:
return 1.0
if n < 0:
x = 1 / x
n = -n

def power(x, n):
if n == 0:
return 1.0
half = power(x, n // 2)
if n % 2 == 0:
return half * half
else:
return half * half * x

return power(x, n)

0 comments on commit de94bab

Please sign in to comment.