Pow(x, n) (Leetcode 50)

Problem Link: https://leetcode.com/problems/powx-n/

class Solution:
    def myPow(self, x: float, n: int) -> float:
        
        def fun(x,n):
            if(n==0):
                return 1
            elif(n==1):
                return x

            temp = fun(x,n//2)
            if(n%2==0):
                return temp*temp
            else:
                return x*temp*temp
            
        if(n<0):
            return 1/fun(x,-n)
        else:
            return fun(x,n)

Last updated