class Solution {
public:
double pow(double x, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n==0)
return 1;
if(x==0)
return 0;
bool negative=false;
if(n<0){
negative=true;
n=~n;
}
double tmp=x;
double result=1;
while(n){
if(n&1==1)
result*=tmp;
tmp*=tmp;
n>>=1;
}
return negative==false?result:1/result/x;
}
};
Leetcode: Pow(x,n)
最新推荐文章于 2025-04-17 16:23:13 发布