lintcode-medium-Pow(x, n)
2016-04-04 14:47
288 查看
Implement pow(x, n).
Notice
You don't need to care about the precision of your answer, it's acceptable if the expected answer and your answer 's difference is smaller than
Example
Challenge
O(logn) time
Notice
You don't need to care about the precision of your answer, it's acceptable if the expected answer and your answer 's difference is smaller than
1e-3.
Example
Pow(2.1, 3) = 9.261 Pow(0, 1) = 0 Pow(1, 0) = 1
Challenge
O(logn) time
public class Solution { /** * @param x the base number * @param n the power number * @return the result */ public double myPow(double x, int n) { // Write your code here if(n == 0) return 1; if(n == 1) return x; if(n > 1){ if(n % 2 == 0){ double temp = myPow(x, n / 2); return temp * temp; } else{ return myPow(x, n - 1) * x; } } else{ return 1 / myPow(x, -n); } } }
相关文章推荐
- Tsinsen A1121 回文数
- POJ-3177-Redundant Paths【双连通分支】
- NYOJ 1036 非洲小孩(水题)
- 学生破解极域电子教室的破解器
- WebView属性示例
- 向Lucene增加中文分词功能
- mysql小笔记
- 【工具】UML工具
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备
- 《leetCode》:Number of Islands
- [转载]C++中类的组合
- Manage Code 托管代码
- matlab simulink 使用总结
- spring security+mybatis+springMVC构建一个简单的项目
- Java安全(加密、摘要、签名、证书、SSL、HTTPS)
- win7命令行常用命令
- C# 中类的成员有哪些?
- 中国气象台api
- 实现Runnable,轻松实现多线程间的资源共享
- 刚接触git,记录一下自己的收获