class Solution { public : double myPow ( double x , int n ) { if ( n == 0 ) return 1 ; if ( x == 1 ) return 1 ; double res = 1 ; long i = n ; //if(n0)i=-n; while ( i ) { if ( i 1 ) res *= x ; //当最低位二进制位为1时,才乘入结果
class Solution {
public:
double myPow(double x, int n) {
if(n==0)return 1;
if(x==1)return 1;
double res=1;
long i=n;
//if(n<0)i=-n;
while(i)
{
if(i&1) res*=x;//当最低位二进制位为1时,才乘入结果
x*=x;//每移动一位,都乘以x
i/=2;//向左移位
}
return n<0? 1/res:res;
}
};