题目描述:

方法一:O(logn)递归:


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

迭代:

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

java版:

class Solution {
public double myPow(double x, int n) {
if(x==0) return 0;
long b = n;
double res = 1.0;
if(b<0){
x = 1 / x;
b = -b;
}
while(b>0){
if((b&1)==1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
}

最新文章

  1. LeetCode Coin Change
  2. 15.6.2 Configuring the Merge Threshold for index pages[innodb]
  3. python数据结构与算法——栈
  4. velocity 显示List和Map方法
  5. mysql导出查询结果到csv方法
  6. json换行符的处理
  7. SeuRain的归来
  8. [转载]SQL字符串处理函数大全
  9. ReentrantLock与synchronized的差别
  10. 【精品】Android游戏类源码大集合
  11. Boost 1.61.0 Library Documentation
  12. 使用OGG&amp;quot;Loading data from file to Replicat&amp;quot;的方法应该注意的问题:replicat进程是前台进程
  13. 学习GDI+ (1)
  14. 3分钟教你做一个iphone手机浏览器
  15. 配置ssh无密钥登陆
  16. [Swift]LeetCode399. 除法求值 | Evaluate Division
  17. ios 适配iOS11&amp;iPhoneX的一些坑
  18. C++的静态联编和动态联编
  19. UNIX环境编程学习笔记(10)——文件I/O之硬链接和符号链接
  20. JS中的getter与setter

热门文章

  1. [已解决]Series object has no attribute explode
  2. 【洛谷】P1247取火柴游戏
  3. Spring入门(四)Spring-test模块
  4. 压缩与解压缩 gzip bzip2 tar 命令
  5. python之next和send用法详解
  6. leetcode-第12周双周赛-5111-分享巧克力
  7. leetcood学习笔记-168-excel表列名称
  8. django 模型ManyToMany 关联的添加,删除,查询
  9. 【luoguP3701】「伪模板」主席树
  10. charles抓取数据