Leetcode||50. Pow(x, n)

来源:互联网 发布:linux 进程网络流量 编辑:程序博客网 时间:2024/06/08 00:15

Implement pow(xn).

实现幂的算法,需要考虑到正负数,我是用递归

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


原创粉丝点击