- Pow
Pow
Implement pow(x, n).
Solution:
1. public class Solution {
2. public double myPow(double x, int n) {
3. if (x == 0) return 0;
4. if (n == 0) return 1;
5. 6. double pow = myPow(x, Math.abs(n) / 2);
7. double res = pow * pow;
8. 9. if ((n & 1) != 0) res *= x;
10. 11. return n < 0 ? 1 / res : res;
12. }
13. }
