[LeetCode#50] Pow(x, n)
2015-09-01 23:52
1291 查看
Problem:
Implement pow(x, n).
Analysis:
Solution:
Implement pow(x, n).
Analysis:
This problem inherently is very easy and simple. Don't try to treat a problem as difficult problem, even the mysterious power operation in mathmatics, could be so easily and elegnatly sovled through algorithm. Soltuion 1. The instant idea is to multiple x one by one until n times. The time complexity of such solution is O(n), which is unacceptable! Naive way: public class Solution { public double myPow(double x, int n) { if (n == 0) return 1; double res = 1; while (n > 0) { res = res * x; n--; } return res; } } Time Limit exceeded: Last executed input: 0.00001, 2147483647 Besides the expensive time cost, there is still a problem with above solution : it could not handle when n is negative. Solution 2: (Recursive way) Since x^4 = x^2 * x^2, apparently, the computational process has a very good recursive characteristic. Why not we do it through recursive way??? Idea: we divide x^n into two parts. but note the n is an odd or even. iff n == odd, x^n = x^(n/2) * x^(n/2) * x; iff n == even, x^n = x^(n/2) * x^(n/2); public class Solution { public double myPow(double x, int n) { if (n == 1) return x; double half = myPow(x, n / 2); if (n % 2 == 0) { return half * half; } else { return half * half * x; } } } The above soltuion seems right, but it still have following pitfalls. 1. cannot handle negative "n". the myPow fuction is based on the assumption: n is positive. Fix: mathematical skills. a^n = (1/a)^(-n) if (n > 0) return power(x, n); else return power(1/x, n); 2. basecase "if (n == 1)" is not useful when n is negative!!! it could incure infinite recursive call. Runtime Error Message: Line 88: java.lang.StackOverflowError Last executed input: 34.00515, -3 Because that reason, can we gurantee the passed in n is positive only? Solution 3: public class Solution { public double myPow(double x, int n) { if (x == 0) return 0; if (n == 0) return 1; if (n > 0) return power(x, n); else return power(1/x, -1 * n); } public double power(double x, int n) { if (n == 1) return x; ... } } The above solution wroks for most case, except when n = Integer.MIN_VAULE = -2147483648 ---------------------------------------------------------------------------------------- Runtime Error Message: Line 91: java.lang.StackOverflowError Last executed input: 1.00000, -2147483648 ******************************** -1 * n could cause overflow!!! Apparently, the valid input range is [2147483647, -2147483648]. We must take care of all possible input! Actually, we just need to make a little change over the code. public double myPow(double x, int n) { ... if (n > 0) return power(x, n); else return power(1/x, n); } public double power(double x, int n) { if (n == 0) return 1; ... } not covert n into positive(overflow)!!! This should be a cautious case all the time!!! never try to covert a negative number into positive number, it could cause overflow problem. The base case if (n == 0) is perfect!!! Thus, during the recursive process, we don't need to care about whether the n is positive and negative, we just care when it would be rounded into 0. (we only care about times, rather than the n's value) Use "0" as base case is really an art! It's equal to: public double power(double x, int n) { if (n == 1 || n == -1) return x; ... }
Solution:
public class Solution { public double myPow(double x, int n) { if (x == 0) return 0; if (n == 0) return 1; if (n > 0) return power(x, n); else return power(1/x, n); } public double power(double x, int n) { if (n == 0) return 1; double half = power(x, n / 2); if (n % 2 == 0) { return half * half; } else { return half * half * x; } } }
相关文章推荐
- 设计模式21:State 状态模式(行为型模式)
- lua面向对象编程
- hdu4122 Alice's mooncake shop(单调队列)
- LeetCode--------------Ugly Number
- iOS程序启动原理
- 设计模式20:Memento 备忘录模式(行为型模式)
- Python3中使用零拷贝技术来提高网络文件传输时的系统吞吐量
- 设计模式19:Chain Of Responsibility 职责链模式(行为型模式)
- 排序算法(Java语言)——快速排序
- Scala学习笔记--Mainfest,ClassTag原理浅析
- Android Studio系列教程一--下载与安装
- 设计模式18:Observer 观察者模式(行为型模式)
- Linux系统环境编程(系统当前时间)
- 学习方法的思考之:“提高学习成绩的七大环节”
- code工具之git
- Ubuntu磁盘分区以及双系统启动顺序修改
- 第40讲:Set、Map、TreeSet、TreeMap操作代码实战
- Layout的一些属性
- 辞职待业的人在家胡思乱想
- 2015第36周二