LC: https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/
牛客: https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&&tqId=11165&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
浮点型 快速幂
将底数转化为 分数计算,确保指数是正数;
class Solution {
public:
double myPow(double x, int n) {
if (x == 1 || n == 0) {
return 1;
}
double sum = 1;
long num = n;
if (n < 0) {
x = 1 / x;
num = -num;
}
while (num) {
if (num & 1) {
sum *= x;
}
num >>= 1;
x *= x;
}
return sum;
}
};