-
|
如题 |
Beta Was this translation helpful? Give feedback.
Answered by
BerBai
May 13, 2023
Replies: 1 comment
-
class Solution {
public double myPow(double x, int n) {
if(n<0){
return 1/pow(x,-n);
} else
return pow(x,n);
}
double pow(double x, int n){
if(n == 0) {
return 1;
}
double v = pow(x, n/2);
if(n%2==0) {
return v*v;
} else {
return v*v*x;
}
}
}快速幂 + 递归,「快速幂算法」的本质是分治算法。 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
taotie6
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
快速幂 + 递归,「快速幂算法」的本质是分治算法。