https://leetcode.com/problems/powx-n/?tab=Description
生成一个pow函数----求幂指数
递归,pow(x, n) == (n % 2) * x * pow(x * x, n / 2)
public class Solution {
public double myPow(double x, int n) {
if (n == 0) {
return 1;
} else if (n < 0) {
n = 0 - n;
return n % 2 != 0 ? ((x * myPow(x * x, n / 2) == 0) ? 0 : 1 / (x * myPow(x * x, n / 2))) : ((myPow(x * x, n / 2) == 0) ? 0 : 1 / myPow(x * x, n / 2));
} else {
return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-9445.html