image.png

解决方案

递归 快速幂

image.png

  1. public double myPow(double x, int n) {
  2. long N=n;
  3. if(N<0){
  4. x= 1/x;
  5. N=-N;
  6. }
  7. return fastPow(x,N);
  8. }
  9. private double fastPow(double x ,long n){
  10. if(n==0)
  11. return 1.0;
  12. double half = fastPow(x, n/2);
  13. if(n%2==0)
  14. return half*half;
  15. else
  16. return half*half*x;
  17. }