Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 544 Bytes

50.-power-x-n.md

File metadata and controls

26 lines (22 loc) · 544 Bytes

50. Power(x, n)

class Solution:
    def myPow(self, x: float, n: int) -> float:
        ## Intuitive 
        return x**n
    ## O(N)

class Solution:
    def myPow(self, x: float, n: int) -> float:
        ## 分类讨论
        ## Corner Case
        if n == 0:
            return 1
        elif n < 0:
            ## 
            x = 1/x
            n = -n      

        if n%2 ==0:
            return self.myPow(x**2,n//2) # 乘子翻倍 
        return x*self.myPow(x,n-1) # 减1 然后变成偶数个
    ## O(logN)