Leetcode 50: Pow(x,n)

Pow(x, n)

Total Accepted: 67450 Total Submissions: 250035 Difficulty: Medium

Implement pow(x, n).

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

 

Code

class Solution(object):
    def myPow(self, x, n):
        """
        :type x: float
        :type n: int
        :rtype: float
        """
        if n==0:
            return 1
        
        tmp = self.myPow(x, abs(n)/2)
        tmp = tmp * x * tmp if n%2 else tmp * tmp
        return tmp if n > 0 else float(1)/tmp 

 

Idea

Please see similar idea at https://czxttkl.com/?p=1000

Leave a comment

Your email address will not be published. Required fields are marked *