[Leetcode]Pow(x, n)
来源:互联网 发布:交换机端口灯不亮 编辑:程序博客网 时间:2024/05/16 08:34
Implement pow(x, n).
用二分法~把x的n次方划分成两个x的n/2次方相乘,然后递归求解~注意n为负数的情况~代码复杂度为O(logn)~ 还可以加上越界的判断~
class Solution: # @param x, a float # @param n, a integer # @return a float def pow(self, x, n): if n == 0: return 1 if n < 0: return self.pow(1.0/x, -n) half = self.pow(x, n >> 1) half *= half if n % 2 == 1: half *= x return half
0 0
- LeetCode: Pow(x, n)
- LeetCode Pow(x,n)
- [Leetcode] Pow(x, n)
- LeetCode: Pow(x, n)
- Leetcode: Pow(x,n)
- [LeetCode] Pow(x, n)
- [Leetcode] Pow(x,n)
- 【leetcode】Pow(x, n)
- [LeetCode]Pow(x, n)
- [leetcode]Pow(x, n)
- LeetCode-Pow(x, n)
- LeetCode - Pow(x, n)
- LeetCode | Pow(x, n)
- Pow(x, n) -leetcode
- 【leetcode】Pow(x, n)
- 【Leetcode】Pow(x, n)
- LeetCode Pow(x, n)
- Leetcode: Pow(x, n)
- 原语组装和光栅化
- 网上订餐的发展现状及前景分析
- Linux下MySQL的启动与停止
- 在android源码环境下写上层应用的一个初步解决方法
- storm安装
- [Leetcode]Pow(x, n)
- 怎样制作网页html
- HDU 4341 Gold miner
- 不同的系统介绍
- 四十九 停电与打牌(下) 我在软件园的那些日子里
- HBase写入操作卡住长时间不返回的原因分析
- 安装SQL Server 2005时,出现“Edition Change Check”警告的解决方法
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
- php---webservice soap