Skip to content

Latest commit

 

History

History
20 lines (15 loc) · 367 Bytes

jian-zhi-offer-16.-shu-zhi-de-zheng-shu-ci-fang.md

File metadata and controls

20 lines (15 loc) · 367 Bytes

剑指 Offer 16. 数值的整数次方

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

        if n == 1:
            return x

        if n%2 == 0 :
            return self.myPow(x*x, n//2)
        return x * self.myPow(x,n-1)