推荐一个详解,不但有思路,而且有代码解释,很nice。
作者:jyd
链接:https://leetcode-cn.com/problems/powx-n/solution/50-powx-n-kuai-su-mi-qing-xi-tu-jie-by-jyd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
个人微改:
public class FastPower {
public static void main(String args[]) {
double a = myPow(3,5);
System.out.println(a);
}
static double myPow(double x,int n) {
long b = n;
double res = 1.0d;
if(x==0.0f) {
return 0.0d;
}
if(n<0) {
x=1/x;
b=-b;
}
while(b>0) {
if((b&1)==1) {
res*=x;
}
x*=x;
b>>=1;
}
return res;
}
}

本文详细介绍了快速幂运算的思路及Java代码实现,通过位运算优化,实现了指数计算的高效求解。适合想要理解并掌握快速幂算法的读者。

被折叠的 条评论
为什么被折叠?



