计算 x 的 n 次幂函数,float类型
思路:分治+递归
class Solution:
def myPow(self, x: float, n: int) -> float:
def helper(N):
if N==0:
return 1.0
y=helper(N//2)
return y*y if N%2==0 else y*y*x
return helper(n) if n>=0 else 1/helper(-n)
class Solution {
public double myPow(double x, int n) {
return n>=0?help(x,n):1/help(x,-n);
}
public double help(double x,int N){
if(N==0){
return 1;
}
double y=help(x,N/2);
return N%2==0?y*y:y*y*x;
}
}