class Solution {
public:
double myPow(double x, int n) {
double ans = 1;
double a = x;
long n1 = n;
int flag = n1<0 ? 1 : 0;
n1 = abs(n1);
while(n1)
{
if(n1&1)
{
ans *= a;
}
a *= a;
n1 >>=1;
}
return flag ? 1/ans : ans;
}
};
剑指 Offer 16. 数值的整数次方
最新推荐文章于 2022-07-05 22:34:45 发布