Difficulty: Medium
Implement , which calculates x raised to the power n (xn).
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
Language: Java
class Solution {
public double myPow(double x, int n) {
if (n == 0) { // 任何数的零次幂都是 1
return 1;
}
if (n < 0) { // 如果 n 是负数,先把负数提到 x 里面,然后再计算
if (n == Integer.MIN_VALUE) {
n++; // 这里需要考虑 INT_MAX < -INT_MIN ,防止溢出
x = x * x;// 同时需要把少乘的那个数字乘进去
}
n = -n; // 这里需要考虑 INT_MAX < -INT_MIN ,所以需要处理一下
x = 1 / x;
}
// 递归
return (n % 2 == 0) ? myPow(x * x, n >> 1) : x * myPow(x * x, n >> 1);
}
}