[LeetCode] Pow(x, n) (二分法)

时间:2023-03-08 18:18:36

Implement pow(x, n).

刚开始没想到,后来看remlost的博客才写出来,代码很简练:

class Solution {
public:
double pow(double x, int n) {
if(n<)
return /power(x,-n);
else
return power(x,n);
}
private:
double power(double x, int n){
if(n==)
return ; double v = power(x,n/);
if(n%==)
return v*v;
else
return v*v*x; } };