原题链接:Leecode 50. Pow(x, n)
关于快速幂的学习博客:快速幂和矩阵快速幂(取模)算法
class Solution {
public:
double myPow(double x, int n) {
if (x==1 || n==0)
return 1;
double res=1;
long long t=n;
if(n<0)
{
x=1.0/x;
t*=-1;//注意范围,n值可取INT_MIN,如果*-1会爆int
}
while(t)
{
if(t&1)
res*=x;
x*=x;
t/=2;
}
return res;
}
};